Java——扩展类并重用方法?

时间:2021-01-09 18:10:00
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()方法,我在许多其他开发人员的代码中看到,他们在超类中重写方法,然后调用super。这有什么原因吗?

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

5 个解决方案

#1


11  

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.

除了构造函数。对于构造函数,确实需要创建自己的签名并向上委托。在这种情况下,省略不是等价的。

#2


8  

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.

重写一个方法,做一些特殊的事情,然后调用super.method()被称为修饰一个方法——您正在添加到行为中。在这里阅读更多:Decorator模式。

Overriding it without calling super's method is simply overriding a method - changing the behaviour.

在不调用super方法的情况下重写它只是重写一个方法—更改行为。

#3


6  

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扩展了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.

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

#4


1  

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

当我想在那里设置断点时,我有时会这样做(临时地,在开发期间)。

#5


0  

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 {

Baseclass(arg1){

Baseclass(__arg1){

}

}

}

}

class A extends BaseClass{

A类扩展BaseClass {

A{ super(arg1); }

{超级(__arg1);}

}

}

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

在其他地方,它只会增加代码的冗余,这根本不是必要的

#1


11  

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.

除了构造函数。对于构造函数,确实需要创建自己的签名并向上委托。在这种情况下,省略不是等价的。

#2


8  

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.

重写一个方法,做一些特殊的事情,然后调用super.method()被称为修饰一个方法——您正在添加到行为中。在这里阅读更多:Decorator模式。

Overriding it without calling super's method is simply overriding a method - changing the behaviour.

在不调用super方法的情况下重写它只是重写一个方法—更改行为。

#3


6  

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扩展了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.

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

#4


1  

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

当我想在那里设置断点时,我有时会这样做(临时地,在开发期间)。

#5


0  

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 {

Baseclass(arg1){

Baseclass(__arg1){

}

}

}

}

class A extends BaseClass{

A类扩展BaseClass {

A{ super(arg1); }

{超级(__arg1);}

}

}

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

在其他地方,它只会增加代码的冗余,这根本不是必要的