好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python实操案例练习(七)

任务1、编写程序实现乐手弹奏乐器

多态

class Instrument():
? ? def make_sound(self):
? ? ? ? pass
class Erhu(Instrument):
? ? def make_sound(self):
? ? ? ? print('二胡在演奏')
class Pinao(Instrument):
? ? def make_sound(self):
? ? ? ? print('钢琴在演奏')
class Violin(Instrument):
? ? def make_sound(self):
? ? ? ? print('小提琴在演奏')
def play(instrumet):
? ? instrumet.make_sound()
class Bird():
? ? def make_sound(self):
? ? ? ? print('小鸟在唱歌')
if __name__=='__main__':
? ? play(Erhu())
? ? play(Pinao())
? ? play(Violin())
? ? play(Bird('小鸟在唱歌'))

任务2、使用面向对象设计自定义类,描述出租车和家用轿车的信息

class Car(object):
? ? def __init__(self,type,no):
? ? ? ? self.type=type
? ? ? ? self.no=no
? ? def start(self):
? ? ? ? pass
? ? def stop(self):
? ? ? ? pass
class Taxi(Car):
? ? def __init__(self,type,no,company):
? ? ? ? super().__init__(type,no)
? ? ? ? self测试数据pany=company
? ? def start(self):
? ? ? ? print('乘客您好!')
? ? ? ? print(f'我是{self测试数据pany}出租车公司的,我的车牌号是{self.no},请问您要去哪里?')
def stop(self):
? ? print('目的地到了,请您付款下车,欢迎下次光临')
class FamillyCar(Car):
? ? def __init__(self,type,no,name):
? ? ? ? super().__init__(type,no)
? ? ? ? self.name=name

? ? def stop(self):
? ? ? ? print('目的地到了,我们去玩儿吧')
? ? def start(self):
? ? ? ? print(f'我是{self.name},我的汽车我做主')
if __name__=='__main__':
? ? taxi=Taxi('上海大众','京A9765','长城')
? ? taxi.start()
? ? taxi.stop()
? ? print('-'*30)
? ? familycar=FamillyCar('广汽丰田','京B88888','武大郎')
? ? familycar.start()
? ? familycar.stop()

到此这篇关于python实操案例练习(七)的文章就介绍到这了,更多相关python实操案例内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于python实操案例练习(七)的详细内容...

  阅读:37次