<<Python基础编程>>
__metaclass__ = type #确定使用新式类
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'Aaaah...'
self.hungry = False
else:
print 'No,thanks!'
class SongBird(Bird):
"""docstring for SongBird"""
def __init__(self):
super(SongBird, self).__init__()
self.sound = 'Squawk!'
def sing(self):
print self.sound
继承新式类(例如object),就不用添加__metaclass__ = type了
class Bird(object):
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print 'Aaaah...'
self.hungry = False
else:
print 'No,thanks!'
class SongBird(Bird):
"""docstring for SongBird"""
def __init__(self):
super(SongBird, self).__init__()
self.sound = 'Squawk!'
def sing(self):
print self.sound