ホームページ  >  に質問  >  本文

python提示SongBird instance has no attribute 'sound'

提示SongBird instance has no attribute 'sound'

_metaclass_ = type
class Bird:
    def _init_(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Aaaaa.....'
            self.hungry = False
        else:
            print 'No, think'

class SongBird(Bird):
    def _init_(self):
        super(SongBird, self)._init_()
        self.sound = 'Squawk!'
    def sing(self):
        print self.sound

sb = SongBird( )
sb.sing( )

刚开始学python,这是书上的代码。检查过很多遍,就是通不过。

高洛峰高洛峰2912日前813

全員に返信(1)返信します

  • 三叔

    三叔2016-10-28 10:15:07

    _metaclass_ = type
    class Bird(object):
        def __init__(self):
            self.hungry = True
        def eat(self):
            if self.hungry:
                print 'Aaaaa.....'
                self.hungry = False
            else:
                print 'No, think'
    
    class SongBird(Bird):
        def __init__(self):
            super(SongBird, self).__init__()
            self.sound = 'Squawk!'
        def sing(self):
            print self.sound
    
    sb = SongBird( )
    sb.sing( )

    __init__不是_init_

    返事
    0
  • キャンセル返事