[英]java explicitly call super method
Are there any reason to call method from super class? 有没有理由从超级班级调用方法?
I have met lots of places where super
method called instead of this
method, eg: 我遇到过很多地方调用
super
方法而不是this
方法,例如:
public String getCustomValue() {
String value = (String) super.getValue(someArgHere);
return value;
}
Any benefits? 有什么好处? I just see one major problem with inheritance: if I override
getValue
in this class or in one of its descendants getCustomValue
will neglect that override and call super
method. 我只看到继承的一个主要问题:如果我在此类或其中一个后代中覆盖
getValue
,则getCustomValue
将忽略该覆盖并调用super
方法。
super.getValue(someArgHere)
calls the getValue
method of the super class. super.getValue(someArgHere)
调用超类的getValue
方法。 In contrast, this.getValue(someArgHere)
calls the getValue
method of the current class, if defined. 相反,
this.getValue(someArgHere)
调用当前类的getValue
方法(如果已定义)。 If the current class does not override getValue
, the super class method is called. 如果当前类没有覆盖
getValue
,则调用超类方法。
Unless you overwrote the method getValue(...)
and you are really sure (your sureness deserves a comment in the code) you want to bypass it you should not use super
like you are doing. 除非你覆盖方法
getValue(...)
并且你确定 (你的确定值得在代码中注释)你想绕过它,你不应该像你正在做的那样使用super
。 Later on if you or someone else decide to overwrite getValue(...)
they probably wanted the effect to apply to getCustomValue()
as well. 稍后,如果您或其他人决定覆盖
getValue(...)
他们可能希望效果也适用于getCustomValue()
。
Although you definitely can call super.myMethodOne()
from myMethodTwo()
, the usual scenario is when you want to call super.myMethodOne()
from myMethodOne()
when you override it. 虽然你绝对可以从
myMethodTwo()
调用super.myMethodOne()
,但通常的情况是你想要在覆盖它时从myMethodOne()
调用super.myMethodOne()
。 Some languages like Ruby even pass up the method arguments automatically for you so that you don't have to retype them. 像Ruby这样的语言甚至会自动为您调出方法参数,这样您就不必重新键入它们。
Here is one example: 这是一个例子:
public class A {
public void close() {
// do a bunch of things...
}
}
public class B extends A {
@Override
public void close() {
// close things related to the subclass B
// and then make sure A is closed as usual...
super.close();
}
}
There are no technical advantages of using super
over this
in the case where the method is not overridden. 有使用没有技术优势,
super
过this
在方法不覆盖的情况。
However, one might say that it's clearer to use super
instead of this
for the reason you've just mentioned. 但是,有人可能会说,出于你刚刚提到的原因,使用
super
而不是this
更清楚。 If you override the function in your subclass, then you will need to use super
; 如果覆盖子类中的函数,则需要使用
super
; if you don't you can use this
. 如果你不这样做,你可以使用
this
。 Instead of playing guessing games (or forcing people to check whether the method has been overridden), you can just always use super
when you mean super
. 而不是玩猜谜游戏(或迫使人们检查方法是否已被重写),你可以随时使用
super
当你的意思是super
。
I just see one major problem with inheritance: if I override
getValue
in this class or in one of its descendantsgetCustomValue
will neglect that override and call super method.我只看到继承的一个主要问题:如果我在此类或其中一个后代中覆盖
getValue
,则getCustomValue
将忽略该覆盖并调用super方法。
Then don't call the super
method explicitly, just call getValue
. 然后不要显式调用
super
方法,只需调用getValue
。 If the method has not been overriden it will default to the super-method. 如果方法尚未被覆盖,则默认为超级方法。 If it has, it will use the overriden method.
如果有,它将使用overriden方法。
I don't know if it's appropriate to ask about "benefits" in this case - it really just depends on what exactly you are trying to accomplish. 我不知道在这种情况下询问“好处”是否合适 - 这实际上取决于你究竟想要完成什么。
The thing is the design. 事情是设计。 When we code, we do it as per what it is!
当我们编码时,我们按照它是什么做的! So even if
getValue
is extended, its perfect, because that is what your class is suppose to do. 因此,即使
getValue
被扩展,它也是完美的,因为这是你的课程所要做的。
Normally, super
is used, to obtain any information or data or side effect from the super type
and modify or improve it as per your current class functionality 通常,使用
super
来从super type
获取任何信息或数据或副作用,并根据您当前的类功能修改或改进它
唯一的好处是如果你的类重写了超类的方法,你仍然可以使用super
调用超类的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.