调用 super 方法是否有意义?
例如:
public class ClassA extends ClassB {
public void run() {
super.execute();
...................
方法execute仅存在于ClassB中。 : 有意义吗
super.execute();
使用:可能就足够了
execute();
? 谢谢。
E.g. :
public class ClassA extends ClassB {
public void run() {
super.execute();
...................
Method execute exists only in ClassB.
Does it make sense to use:
super.execute();
may be enough:
execute();
?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于
ClassA
中没有重写execute()
方法,因此execute()
与super 等价 .execute()
。我认为强调该方法是在超类中定义的事实绝对没有“文档”好处。因此,就我个人而言,我会编写
execute()
(除非有充分的理由在ClassA
被修改为覆盖classA
时调用super.execute()
代码>执行)。Since the
execute()
method is not overridden inClassA
,execute()
is equivalent tosuper.execute()
.I see absolutely no "documentational" benefit of highlighting the fact that the method is defined in the super class. Thus personally I would write
execute()
(unless there were strong reasons to callsuper.execute()
ifClassA
was ever modified to overrideexecute
).原因:
可预测性(安全?) - 如果有一天有人实现
ClassA.execute()
,代码的工作方式将会有所不同。即使有人子类化ClassA
并覆盖execute()
,保持ClassA
完好无损,情况也是如此。性能 - 超级调用可能比虚拟更快。
super
是使用invokespecial
实现的(单次调度,就像private
方法),而普通调用非私有方法则使用双重调度(虚拟调用)。这是现代 JVM 的一个弱优势。底线:如果
ClassB.execute()
是final
,则使用super
没有任何意义。Reasons:
Predictability (safety?) - if one day somebody implements
ClassA.execute()
, the code will work differently. This is also the case even is somebody subclassesClassA
and overridesexecute()
, leavingClassA
intact.Performance - super call may be faster than virtual.
super
is implemented usinginvokespecial
(single dispatch, just likeprivate
method), while ordinary call to non-private method uses double dispatch (virtual call). This is a weak advantage in modern JVMs.Bottom line: if
ClassB.execute()
isfinal
, usingsuper
has no sense.不,只需调用
execute()
即可。由于方法未被重写,super.execute()
将具有与this.execute()
相同的结果,这与执行()
。No, simply call
execute()
. Since the method is not overridden,super.execute()
would have the same result asthis.execute()
, which is the same asexecute()
.调用
super
就可以了。如果您想确保调用父类方法,请使用
super
。因此,如果您稍后在 ClassA 中引入该方法,代码将不会中断。相反,如果您想调用
execute
,无论它是什么......不要使用super
。super
保留字通常存在,用于从重写方法调用重写方法。It's ok to call
super
.If you want to ensure you are calling the parent class method, then use
super
. So if you introduce the method later inClassA
, code will not break.By the contrary, if you want to call
execute
whatever it is... don't usesuper
.super
reserved word exist normally to call to the overriden method from the overrider one.根据java语法这是正确的。但当涉及到真正的软件工程概念时,这根本没有任何意义。我建议您重写 B 类中的 run 方法,然后调用其中的超类方法。调用超级类方法后,您可以为子类构造特定的逻辑。就像您在父方法中具有一些通用功能,在子类中具有特定功能(除了父方法之外)。
According to the java syntax this is correct. but when it comes to the real software engineering concepts this wont make any sense at all.I would suggest you to override run method in class B and then call the super class method inside that. After calling the supper class method you can construct you specific logic for the subclass. Its like you have some generic functionalities in the parent method and specific functionalities (in additions to what parent has) in sub class.