我在尝试我的超级函数,下面是我正在执行的代码。你知道吗
class scene(object):
def enter(self):
print "a vllan s n your way. what you'll do?"
class centralcorrdor(scene):
print "startng pont of the game."
super(centralcorrdor,self).enter()
a = centralcorrdor()
但是这会产生错误。你知道吗
class centralcorrdor(scene):
File "game.py", line 8, in centralcorrdor
super(centralcorrdor,self).enter()
NameError: name 'centralcorrdor' is not defined
但事实并非如此。你知道吗
class scene(object):
def enter(self):
print "a vllan s n your way. what you'll do?"
class centralcorrdor(scene):
#print "startng pont of the game."
def func(self):
super(centralcorrdor,self).enter()
#scene.enter()
a = centralcorrdor()
a.func()
有人能告诉我为什么吗?是不是在子类的方法内部调用了super?你知道吗
必须在方法中使用super。有关更多信息,请咨询: python-programming或using-super-with-a-class-method 我希望这对你有帮助;)
super
实际上是基类的代理。有类代理(在静态或类方法中定义)和实例代理(在实例方法中定义)。你知道吗您的
super(centralcorrdor,self).enter()
语句传递self作为对象参数,如果您选中enter
签名就是self
。因此,必须在实例对象上调用它,而不是在类方法中。你知道吗一般来说,大多数时候您都会调用
super(class, object)
。你知道吗相关问题 更多 >
编程相关推荐