构建一个实现以下需求的程序。示例居中 关于音乐:
我试过这样做,但我被困在班上,我不知道下一步该怎么办
class Instrument:
sound: str
def __init__(self, type, sound):
self.type = type
self.sound = sound
def Playinstrument(self):
return f"This {self.type} sounds like {self.sound}"
class Piano(Instrument):
numberofkeys: int
def __init__(self, type, sound, numberofkeys ):
self.type = type
self.sound = sound
self.numberofkeys = numberofkeys
def Playinstrument(self):
return f"this {self.type} plays beautiful music on these {self.numberofkeys} keys"
class Violin(Instrument):
def __init__(self, type, sound):
self.type = type
self.sound = sound
def Playinstrument(self):
return print("beautiful music")
class Person:
name: str
def __init__(self, name = ""):
self.name = name
def __repr__(self):
return self.name
class musician(Intstrument):
musician: str
def __init__(self, )
基本上,您必须严格遵循以下说明:
Instrument
有一个type
,有一个sound
,和plays
声音(打印字符串,实际上不播放音乐)李>Piano
是一个Instrument
,有一个number of keys
,和plays
“美妙的音乐”这许多键(打印字符串,不实际播放音乐)李>Violin
是一个Instrument
和plays
“优美的音乐”(打印字符串,实际上不播放音乐)李>Person
有一个name
李>Musician
是一个拥有Instrument
和plays
这个Instrument
的Person
李>在这里,我将每个描述作为
class
的docstring
;然后我按照指示去做输出:
相关问题 更多 >
编程相关推荐