繁体   English   中英

从超类的超类调用方法

[英]Call a method from superclass of superclass

我有一个例子。 我有两节课。 AB的超类。

class A(object):
   def p(self):
      print "a"


class B(A):
   def p(self):
      print "b"
      super(A, self).p()

现在我有一个新的C类。 CB的子类,应该再次覆盖方法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
   ...

我怎样才能覆盖方法p()父类B在类C并调用该方法p()supersuperclassA 而不调用的方法p超类的B 有人知道吗?

P / S:方法名称必须相同。

只需直接调用Ap(self) ,这不是super()旨在支持的功能。 请注意,您必须手动传入self ,因为Ap()是一种未绑定的方法。

但是,您想重新考虑您的类设计。 也许您想介绍辅助方法; 然后可以从Ap()Cp()调用其中之一,但不能从Bp()调用吗? 这样, Bp()就不必使用super().p() 所有,而只是简单地重新使用这些辅助方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM