【发布时间】:2020-02-15 04:15:50
【问题描述】:
我正在开发相互继承的不同对象。
有一次,我注意到超级方法的代码仍然由继承的方法执行。
从下面列出的子方法可以看出,我注释了super(),以防止超类代码被执行。
这是超类:
class superClass(superClassOfSuperClass):
def __init__(self, data):
#[...]
#unique call of the superMethod
self.component.addActionListener(self.superMethod)
#[...]
def superMethod(self, param):
print "I'm executed, but I don't have to be executed!"
这是子方法:
class subClass(superClass):
def __init__(self, data, newData):
superClass.__init__(self, data)
self.newData = newData
#[...]
def superMethod(self, param):
#super(superClass, self).superMethod(None)
print 'only I have to be printed!!!'
代码有效。
但我不想看到superMethod 打印。
有没有办法阻止 superMethod 运行并使 subMethod 只执行?
因为到目前为止我可以看到两个打印输出。
我希望这是我在 OOP 上的不足。
PS:正如您从标签中看到的那样,我正在使用 Jython 2.7。
【问题讨论】:
-
您的呼叫站点是什么样的?顺便说一句,
superMethod和subMethod是不同的名称,通常不会相互覆盖.. -
已更正。该调用实际上是在超类中进行的。是这个原因吗?我应该重写子类的
__init__吗? -
您的呼叫站点是什么样的?
-
是超类的
__ init__。我称之为:self.component.addActionListener(self.superMethod)这是我唯一调用 superMethod 的站点 -
好的,这很有趣。请使用此新信息更新问题。事实上,请同时显示
__init__方法。
标签: python python-2.7 jython jython-2.7