class Animal(object):
def __init__(self, nlegs=4):
print 'in __init__ Animal'
self.nlegs = nlegs
class Cat(Animal):
def __init__(self, talk='meow'):
print 'in __init__ Cat'
super(Cat, self).__init__()
self.talk = talk
tom = Cat()
print "I'm a cat with {} legs and I say '{}'".format(tom.nlegs, tom.talk)
self
是隐式传递的,因此您实际上分别传递了tower、gun(即self)和place。在另外,您应该在这里使用super函数,而不是显式地调用
tower.__init__
。在使用super的示例(python 2.7):
相关问题 更多 >
编程相关推荐