实现思路及关键代码:
1)定义乐器类Instrument,包括makeSound()方法,此方法中乐器声音:"乐器发出美妙的声音!"
2)定义乐器类的子类:二胡Erhu、钢琴Piano和小提琴Violin
二胡Erhu声音:"二胡拉响人生"
钢琴Piano声音:"钢琴美妙无比"
小提琴Violin声音:"小提琴来啦"
3)用main类,多态的方式对不同乐器进行切换
class Instrument:
def makeSound(self):
print('乐器声音:乐器发出美妙的声音!')
class Erhu(Instrument):
def makeSound(self):
print('二胡Erhu声音: 二胡拉响人生')
class Piano(Instrument):
def makeSound(self):
print('钢琴Piano声音: 钢琴美妙无比')
class Violin(Instrument):
def makeSound(self):
print('小提琴Violin声音: 小提琴来啦')
def fun(obj):
obj.makeSound()
if __name__ == '__main__':
a1 = Erhu()
a2 = Piano()
a3 = Violin()
fun(a1)
fun(a2)
fun(a3)