简体   繁体   English

用这个。 或超级。 调用存在于超类和子类中的方法时

[英]Using this. or super. when calling a method that exist in a superclass and a subclass

There is a class called Champion who have an array of Skill as an instance variable. 有一个名为Champion的类,它有一个Skill数组作为实例变量。

It implement a method called "public final Skill[] getSkills()" where Skill[] is an array of another object called Skill. 它实现了一个名为“public final Skill [] getSkills()”的方法,其中Skill []是另一个名为Skill的对象的数组。

There is a subclass called Support that implement a method called "public final boolean canHeal()" that check the array of skills in the method canHeal. 有一个名为Support的子类实现了一个名为“public final boolean canHeal()”的方法,它检查方法canHeal中的技能数组。

Should I write" Skill[] x = this.getSkills() " or : Skill[] x = super.getSkills() " to get the array of Skill and why ? 我应该写“Skill [] x = this.getSkills()”或:Skill [] x = super.getSkills()“来获取技能数组以及为什么?

Keep in mind that the method getSkills is only defined in class Champion and not overridden in class Support. 请记住,方法getSkills仅在类Champion中定义,而不在类Support中重写。

Thanks in advance. 提前致谢。

The super keyword allows you to call the base class' version of a method that your class overrode or shadowed. super关键字允许您调用类重写或镜像的方法的基类版本。

If you class doesn't have a separate getSkills() method, super will have no effect. 如果你的类没有单独的getSkills()方法,那么super将没有任何效果。

I think it is better to call the function without super . 我认为最好在没有super情况下调用该函数。

First of all - for now there is now difference. 首先 - 目前现在存在差异。 In the future, if you'll override this function in your class it will be hard to spot (or at least need attention). 将来,如果你在课堂上重写这个功能,很难发现(或者至少需要注意)。

In case you override your function, you might want to call super.getSkills() inside it. 如果你覆盖你的函数,你可能想在其中调用super.getSkills()

You should do neither. 你不应该这样做。 Adding this. 添加this. is just superfluous and adding super. 是多余的,加入super. is likely to break your class if in the future you override getSkills() . 如果在将来你覆盖getSkills()可能会破坏你的课程。 So just do: 所以这样做:

Skill[] x = getSkills();

In the current situation it will simply call the method defined in the superclass, if in the future you have a reason to override getSkills() (eg because your class uses a different way, or has additional conditions), it will be used automatically; 在当前情况下,它将简单地调用超类中定义的方法,如果将来您有理由覆盖getSkills() (例如,因为您的类使用不同的方式,或具有其他条件),它将自动使用; which is more likely to be the correct behavior than continuing to call the superclass method. 这比继续调用超类方法更可能是正确的行为。

In general you should only call super.someMethod() inside the overridden method if you need it (eg to not duplicate some logic or side-effects). 通常,如果需要,只应在重写方法中调用super.someMethod() (例如,不要复制某些逻辑或副作用)。 Using it in another method should only be done if you are sure that you don't want your own implementation, but that of your superclass. 只有在确定不需要自己的实现时,才应该在另一种方法中使用它,而不是超类的实现。

我认为你可以使用,因为支持是冠军,但使用super.getSkills会使代码更清晰,你在超类中调用一个方法。

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

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