使用音乐音乐会python创建一个类

2024-06-28 20:34:43 发布

您现在位置:Python中文网/ 问答频道 /正文

构建一个实现以下需求的程序。示例居中 关于音乐:

  • 类乐器有一个类型,有一个声音,并播放那个声音(打印一个字符串,不要 实际播放音乐)
  • 课堂钢琴是一种乐器,有多个键,能演奏“美妙的音乐” 这是许多键(打印字符串,实际上不播放音乐)
  • 班上的小提琴是一种乐器,演奏“优美的音乐”(打印一根弦,实际上不需要) 播放音乐)
  • 一个人有一个名字
  • 音乐家是拥有一种乐器并演奏这种乐器的人
  • 一个管弦乐队有一个音乐家名单,并在所有音乐家演奏的地方演奏一场音乐会 音乐

我试过这样做,但我被困在班上,我不知道下一步该怎么办

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, ) 

Tags: nameselfreturn音乐initdeftypeclass
1条回答
网友
1楼 · 发布于 2024-06-28 20:34:43

基本上,您必须严格遵循以下说明:

  • Instrument有一个type,有一个sound,和plays声音(打印字符串,实际上不播放音乐)
  • Piano是一个Instrument,有一个number of keys,和plays“美妙的音乐”这许多键(打印字符串,不实际播放音乐)
  • Violin是一个Instrumentplays“优美的音乐”(打印字符串,实际上不播放音乐)
  • 一个Person有一个name
  • aMusician是一个拥有Instrumentplays这个InstrumentPerson
  • 一个{}有一个{}{}和{}的音乐会,所有的音乐家都会演奏音乐

在这里,我将每个描述作为classdocstring;然后我按照指示去做

class Instrument:
    """has a type, has a sound, and plays that sound 
    (print a string, don't actually play music)
    """
    def __init__(self, instrument_type, sound):
        self.instrument_type = instrument_type
        self.sound = sound
        
    def play(self):
        print(f'{self.instrument_type}, and plays {self.sound}', end=' ')
      

class Piano(Instrument):
    """an instrument, has a number of keys, and plays 
    'beautiful music on ' this many keys"""
    def __init__(self, num_keys):
        super().__init__('Piano', 'beautiful music')
        self.num_keys = num_keys
        
    def play(self):
        super().play()
        print(f'on {self.num_keys} keys', end=' ')
        
  
class Violin(Instrument):
    """an instrument and plays 'beautiful music'
    """
    def __init__(self):
        super().__init__('Violin', 'beautiful music')
    
    # uses the `play` method of the superclass

    
class Person:
    """a Person has a name.
    """
    def __init__(self, name):
        self.name = name


class Musician(Person):
    """a musician is a person who has an instrument 
    and plays this instrument
    """
    def __init__(self, name, instrument):
        super().__init__(name)
        self.instrument = instrument
        
    def play(self):
        print(f'{self.name} is playing the', end=' ')
        self.instrument.play()
       

class Orchestra:
    """an orchestra has a list of musicians and 
    plays a concert where all musicians play music
    """
    def __init__(self, musicians):
        self.musicians = musicians[:]
    def play(self):
        for musician in self.musicians:
            musician.play()
            print()
        

paul = Musician('Paul', Piano(840))   # that's a big Piano! ;-)           
mary = Musician('Mary', Violin())              
jack = Musician('Jack', Violin())  

orchestra = Orchestra([paul,mary, jack])
orchestra.play()

输出:

Paul is playing the Piano, and plays beautiful music on 840 keys 
Mary is playing the Violin, and plays beautiful music 
Jack is playing the Violin, and plays beautiful music 

相关问题 更多 >