27_python实操案例十三
任务一:
class Instrument(): def make_sound(self): pass class Erhu(Instrument): def make_sound(self): print('二胡在演奏') class Piano(Instrument): def make_sound(self): print('钢琴在演奏') class Violin(Instrument): def make_sound(self): print('小提琴在演奏') # 演奏的函数 def play(instrument): instrument.make_sound() class Bird(): def make_sound(self): print('小鸟在唱歌') if __name__ == '__main__': play(Erhu()) play(Piano()) 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 FamilyCar(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},我的车牌号是{self.no},我的汽车我做主') if __name__ == '__main__': taxi = Taxi('上海大众', '京A 9 3 5 3 2', '长城') taxi.start() taxi.stop() print('-'*30) familycar = FamilyCar('广汽丰田', '京B888888', '武大郎') familycar.start() familycar.stop()

浙公网安备 33010602011771号