时间:2022-09-10 09:19:51 | 栏目:Python代码 | 点击:次
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('小鸟在唱歌'))
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.company=company def start(self): print('乘客您好!') print(f'我是{self.company}出租车公司的,我的车牌号是{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()