
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(instrument):
instrument.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 Taix(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},我得汽车我做主')
if __name__ == '__main__':
taxi = Taix('上海大众','京A 1234','长城')
taxi.start()
taxi.stop()
print('--'*30)
family = FamilyCar('宝马','京B 4321','张三')
family.start()
family.stop()
'''打印:
乘客您好!
我是长城出租车公司的,我得车牌是京A 1234,请问您要去哪里?
目的地到了,请付费下车!
------------------------------------------------------------
我是张三,我得汽车我做主
目的地到了,我们去玩吧!
'''