Java - 扩展类并重用方法?
public class BaseClass {
public void start() {
// do something
}
}
public class ClassA extends BaseClass {
}
ClassA c = new ClassA();
c.start();
在下面的代码中,我想使用在超类中定义的 start() 方法,我在很多其他开发人员的代码中看到他们重写了超类中的方法,然后调用了 super.start() 方法。这是有原因的吗?
public class ClassA extends BaseClass {
@Override
public void start() {
super.start();
}
}
public class BaseClass {
public void start() {
// do something
}
}
public class ClassA extends BaseClass {
}
ClassA c = new ClassA();
c.start();
In the following code I want to use the start() method as it was defined in the super class, I have seen in a lot of other developer's codes that they override the method in the super class and then they call the super. is there a reason for that?
public class ClassA extends BaseClass {
@Override
public void start() {
super.start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
明晰?一些开发人员认为在子类中显示方法更清晰。我不同意。这是多余的信息。
至少从 Java 5 开始,您可以添加 @Override,以便编译器会告诉您签名是否更改/消失。
除了构造函数。对于构造函数,您确实必须使用相同的签名创建自己的构造函数并向上委托。在这种情况下,省略并不等同。
Clarity? Some developers feel it is clearer to show the method in the subclass. I disagree. It's redundant info.
At least since Java 5, you could add an @Override so the compiler will tell you if the signature changes/disappears.
Except for constructors. For constructors, you really do have to create your own with the same signature and delegate upwards. In this case, omitting isn't equivalent though.
重写一个方法,做一些特殊的事情,然后调用
super.method()
被称为装饰一个方法 - 您正在添加到行为中。在这里阅读更多内容:装饰器模式。在不调用 super 方法的情况下覆盖它只是覆盖一个方法 - 改变行为。
Overriding a method, doing something special, then calling
super.method()
is called decorating a method - you're adding to the behaviour. Read more here: Decorator Pattern.Overriding it without calling super's method is simply overriding a method - changing the behaviour.
所做的事情与根本不覆盖完全相同
public class ClassA extends BaseClass {}
因此,除非您需要添加一些额外的功能(在这种情况下,您调用超类的方法,然后添加额外的逻辑)或者做一些不同的事情(你不调用超类的方法,只是定义一些不同的逻辑),最好不要重写超类的方法(并调用超类的方法),因为它毫无意义。
does exactly the same thing as not overriding at all like this
public class ClassA extends BaseClass {}
So unless you have some extra functionality to add (in which case you call super class's method and then add your extra logic) or to do something different(you don't call super class's method and just define some different logic), it's best you don't override super class's method (and call super class's method) because it's pointless.
当我想在那里设置断点时,我有时会这样做(暂时在开发过程中)。
I sometimes do this (temporarily, during development) when I want to set a break-point there.
继承概念背后的主要原因是子类之间通用的函数或操作的泛化。仅当我们需要为特定子类自定义操作时,重写才有意义。
例如
,它只会添加冗余的代码,这根本没有必要
The main reason behind the concept of inheritance is generalization of the functions or operations that are common among sub classes. It makes sense to override only when we need to customize the operation for the particular sub-class.
For example
In other places it would only add a redundancy of the code, which is not at all necessary