Java - 扩展类并重用方法?

发布于 2024-11-17 02:03:50 字数 415 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

勿忘初心 2024-11-24 02:03:50

明晰?一些开发人员认为在子类中显示方法更清晰。我不同意。这是多余的信息。

至少从 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.

海风掠过北极光 2024-11-24 02:03:50

重写一个方法,做一些特殊的事情,然后调用 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.

能怎样 2024-11-24 02:03:50
public class ClassA extends BaseClass {
  @Override
  public void start() {
     super.start();
  }
}

所做的事情与根本不覆盖完全相同

public class ClassA extends BaseClass {}

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

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 {}

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.

走过海棠暮 2024-11-24 02:03:50

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

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

会发光的星星闪亮亮i 2024-11-24 02:03:50

继承概念背后的主要原因是子类之间通用的函数或操作的泛化。仅当我们需要为特定子类自定义操作时,重写才有意义。

  • 但是我们确实需要进行覆盖的一个地方是,假设您已经重载了超类默认构造函数,那么在子类中您需要将重载的超类构造函数的调用作为子类中的第一行构造函数(即必须在创建子类之前创建超类对象)。
    例如

类基类{

基类(arg1){

}

}

A 类扩展了 BaseClass{

一个{
超级(arg1);
}

}

,它只会添加冗余的代码,这根本没有必要

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);
}

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文