qvp*_*ham 2 python superclass python-2.7
我有一个例子.我有两节课.A是超类B.
class A(object):
def p(self):
print "a"
class B(A):
def p(self):
print "b"
super(A, self).p()
Run Code Online (Sandbox Code Playgroud)
现在我有了一堂新课C.该类C是子类,B并且应该p再次覆盖该方法,如下所示:
class C(B):
def p(self):
print "c"
# here call the method p of the 'supersuper'class A
# without calling the method p of the superclass B
...
Run Code Online (Sandbox Code Playgroud)
我怎么能覆盖方法p()中的超 B类中C并调用该方法p()的的supersuperclass A 而不调用方法p的超类的B?有人有想法吗?
P/S:方法名称必须相同.
只是A.p(self)直接打电话,这不是super()设计支持的.请注意,您必须self手动传入,这A.p()是一种未绑定的方法.
但是,您想重新考虑您的课程设计.也许你想引入辅助方法; 其中一个可以从A.p(),C.p()但不是B.p()?这样B.p()就不必用super().p() 在所有的,而只是简单地重新使用这些辅助方法.