您的位置:首页 > 其它

使用super函数

2016-01-08 10:13 225 查看
<<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: