1)乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin)
2)各种乐器的弹奏( play )方法各不相同。
3)编写方法testPlay,对各种乐器进行弹奏测试。
4)要依据乐器的不同,进行相应的弹奏。
class Instrument(object):#定义一个可以被继承的类
def __init__(self, instrument_type):#构造对象时自动调用
self.instrument_type=instrument_type#默认公共属性
def play(self):
print('弹奏方式',self.instrument_type)
class Piano(Instrument):#子类继承父类
def play(self):
print('弹奏方式:弹',self.instrument_type)#子类独有函数
class Violin(Instrument):#子类继承父类
def play(self):
print('弹奏方式:拉、弹',self.instrument_type)#子类独有函数
def testPlay():
#构造对象
piano=Piano('钢琴')
piano.play()
violin=Violin('小提琴')
violin.play()
testPlay()#调用方法