简体   繁体   English

在不需要时使用超级有什么危害吗?

[英]Is there any harm in using super when not needed?

This message pertains strictly to Java. 此消息严格适用于Java。 If a method is in a superclass there are two ways the method could be called: 如果方法在超类中,则可以通过两种方式调用该方法:

foo();
super.foo();

Is there any harm in always doing the latter? 总是做后者有什么害处吗? As a coding style I prefer the latter because it's clear at a glance where the method call is coming from. 作为一种编码风格,我更喜欢后者,因为一眼就能看出方法调用的来源。 Are there any circumstances where 'super' is going to be non-existent or not do what I think it would do? 在任何情况下,“超级”将不存在或不做我认为会做的事情吗?

I think the only harm you may have is when you want to use polymorphism so, if you call foo() and some subclass overrides foo, then the effect would be different than if you call super.foo() , basically, it depends on how you are designing the code and for what purpose. 我认为你可能遇到的唯一伤害是当你想使用多态时,如果你调用foo()并且某些子类重写foo,那么效果会比你调用super.foo()有所不同,基本上,它取决于你是如何设计代码以及用于何种目的的。

Hope this makes it clear. 希望这说清楚。

As a general rule, you should only use super.foo() inside your class foo() method. 作为一般规则,您应该只在类foo()方法中使用super.foo() Doing otherwise, in general, goes against OOP thinking. 总的来说,不这样做违背了OOP的思想。

because it's clear at a glance where the method call is coming from 因为一眼就可以看出方法调用的来源

In OOP you should'n want to know where the method call "comes from". 在OOP中,您不应该知道方法调用“来自”的位置。 If your program (or your thinking) is depending on that, you are in potential trouble (and will probably be in actual trouble when someone decides to override the method). 如果您的程序(或您的想法)依赖于此,您可能会遇到麻烦(当有人决定覆盖该方法时,可能会遇到实际问题)。 The method myobject.foo() must be seen from the outside as the method of the myobject's class; myobject.foo()方法必须从外部看作myobject类的方法; it should not matter if that method is implemented actually in the concrete class of its parent. 如果该方法实际上在其父级的具体类中实现,则无关紧要。

我会说做前者有更多的危害,因为它不是一个超类的方法。

Yeah this basically breaks the inheritance chain. 是的,这基本上打破了继承链。

You don't allow the inheritance mechanism to choose what function to use even in classes derived from this one. 您不允许继承机制选择即使在从此派生的类中使用的函数。

The point of super.foo() is to allow you to specify only when it is needed and you know no other behavior will be good. super.foo()是允许您仅在需要时指定,并且您知道其他任何行为都不会很好。

If you do not want to explicitly avoid using an overriding method in the subclass then you should not use super. 如果您不想明确避免在子类中使用重写方法,那么您不应该使用super。 Always using super might cause trouble if later on someone wants to override the method in the subclass. 如果以后某人想要覆盖子类中的方法,那么总是使用super可能会造成麻烦。

That's a preferable way if you intend to call the method on the super class, instead of calling foo() without super. 如果您打算在超类上调用该方法,而不是在没有super.情况下调用foo() ,那么这是一种更好的方法super. . If anyone does overwrite foo() in the subclass the super call does call the same method as before, but omiting super will now call the overwritten method. 如果有人在子类中覆盖foo() ,则超级调用会调用与之前相同的方法,但是省略super将现在调用覆盖的方法。 It depends on what you intent with that method call. 这取决于您对该方法调用的意图。

It depends. 这取决于。

If foo() is declared as final , it will make no difference at all. 如果foo()被声明为final ,那么它将没有任何区别。 If foo() is not declared as final , then a subclass could override the declaration of foo() in your superclass and completely change the expected behaviour. 如果foo() 声明为final ,则子类可以覆盖超类中foo()的声明,并完全更改预期的行为。

If you make your own class final , you can prevent it from being sub-classed, and be certain the original intent is preserved. 如果您将自己的类设为final ,则可以防止它被分类,并确保保留原始意图。

I would be say that this might indicate that you should think over you design again. 我会说这可能表明你应该再考虑一下你的设计。

If you are always calling the functionality by super.foo() then you block yourself from overriding the function later, and if you don't want to have the ability to override the function, then maybe you shouldn't use inheritance as a method for accessing this functionality. 如果你总是通过super.foo()来调用这个功能,那么你可以阻止自己以后覆盖这个函数,如果你不想重写函数,那么也许你不应该使用继承作为方法用于访问此功能。

One design principle that I have heard banded about is "favour composition over inheritance", the reason for this is that your code becomes more flexible with composition rather than inheritance. 我听说过的一个设计原则是“赞成组合而不是继承”,其原因是你的代码变得更灵活,而不是继承。 And if you don't gain the positive aspects of inheritance (being able to override the function) then maybe it's wiser to not use inheritance. 如果你没有获得继承的积极方面(能够覆盖函数),那么可能不使用继承更明智。

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

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