Java - 扩展类并重用方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:09:59  来源:igfitidea点击:

Java - extending a class and reusing the methods?

javaclassextendinheritance

提问by aryaxt

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?

在下面的代码中,我想使用在超类中定义的 start() 方法,我在很多其他开发人员的代码中看到,他们覆盖了超类中的方法,然后调用了超类。有什么原因吗?

public class ClassA extends BaseClass {
   @Override
   public void start() {
      super.start();
   }
}

采纳答案by Jeanne Boyarsky

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.

至少从 Java 5 开始,您可以添加 @Override 以便编译器会告诉您签名是否更改/消失。

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.

构造函数除外。对于构造函数,您确实必须使用相同的签名并向上委托创建自己的构造函数。在这种情况下,省略并不等同。

回答by Bala R

public class ClassA extends BaseClass {
  @Override
  public void start() {
     super.start();
  }
}

does exactly the same thing as not overriding at all like this

和完全不覆盖完全一样

public class ClassA extends BaseClass {}

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.

所以除非你有一些额外的功能要添加(在这种情况下你调用超类的方法然后添加你的额外逻辑)或者做一些不同的事情(你不调用超类的方法而只是定义一些不同的逻辑),最好你不要覆盖超类的方法(并调用超类的方法),因为它毫无意义。

回答by Bohemian

Overriding a method, doing something special, then calling super.method()is called decoratinga method - you're addingto the behaviour. Read more here: Decorator Pattern.

覆盖一个方法,做一些特别的事情,然后调用super.method()被称为装饰一个方法——你正在添加到行为中。在这里阅读更多:装饰模式

Overriding it without calling super's method is simply overridinga method - changingthe behaviour.

在不调用 super 方法的情况下覆盖它只是覆盖一个方法 -改变行为。

回答by Thilo

I sometimes do this (temporarily, during development) when I want to set a break-point there.

当我想在那里设置断点时,我有时会这样做(暂时,在开发过程中)。

回答by A.R.Venkatesh palani prabhu

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.

继承概念背后的主要原因是子类之间通用的功能或操作的泛化。仅当我们需要为特定子类自定义操作时才进行覆盖。

  • But one place where we do need to make an override is, say you have overloaded your super class default constructor, then in sub-class you need to mention the invocation of the overloaded super-class constructor as the first line in your sub-class constructor(ie Super-class object has to be created before sub-class creation). For example
  • 但是我们确实需要进行覆盖的一个地方是,假设您已经重载了您的超类默认构造函数,然后在子类中您需要提及重载超类构造函数的调用作为您子类中的第一行构造函数(即必须在创建子类之前创建超类对象)。例如

class BaseClass{

Baseclass(arg1){

}

}

class A extends BaseClass{

A{ super(arg1); }

}

类基类{

基类(arg1){

}

}

A 类扩展 BaseClass{

A{ 超级(arg1); }

}

In other places it would only add a redundancy of the code, which is not at all necessary

在其他地方,它只会增加代码的冗余,这完全没有必要