1. 老式类中会使用到的
2. 新式类中会使用到的
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):
def __init__(self):
Bird.__init__(self)
self.sound = 'Squawk!'
def sing(self):
print self.sound
2. 新式类中会使用到的
__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):
def __init__(self):
super(SongBird, self).__init__()
self.sound = 'Squawk!'
def sing(self):
print self.sound
老式与新式类的区别及应用
本文对比了老式类和新式类在Python中的实现方式,并通过实例展示了它们在不同场景下的应用,包括类的初始化、方法定义以及继承机制。
2945

被折叠的 条评论
为什么被折叠?



