代码如下
#! /usr/bin/env python
_metaclass_ = type
class bird:
def _init_(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'I am hungry'
self.hungry = False
else:
print 'No,thanks'
class Songbird(bird):
def _init_(self):
super(Songbird, self)._init_()
self.sound = 'Squawk!'
def sing(self):
print self.sound
a = Songbird()
a.sing()
a.eat()
a.eat()
错误信息:
Traceback (most recent call last):
File "bird.py", line 21, in <module>
a.sing()
File "bird.py", line 18, in sing
print self.sound
AttributeError: Songbird instance has no attribute 'sound'
#! /usr/bin/env python
_metaclass_ = type
class bird:
def _init_(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'I am hungry'
self.hungry = False
else:
print 'No,thanks'
class Songbird(bird):
def _init_(self):
super(Songbird, self)._init_()
self.sound = 'Squawk!'
def sing(self):
print self.sound
a = Songbird()
a.sing()
a.eat()
a.eat()
错误信息:
Traceback (most recent call last):
File "bird.py", line 21, in <module>
a.sing()
File "bird.py", line 18, in sing
print self.sound
AttributeError: Songbird instance has no attribute 'sound'