简体   繁体   English

在子类的方法中本地调用超类方法时,使用“ super”关键字还是使用超类实例?

[英]Using “super” keyword or using a superclass instance when calling superclass methods locally in a method from subclass?

Let say I have: 假设我有:

class Superclass {
  //fields...

  methodA() {...}

  methodB() {...}

...
}

class Subclass extends Superclass {
   //fields...

   methodA() {
      // Here I need to call methods A and B from superclass:
      // For this, I can use supper
      super.methodA();
      super.methodB();

      // Or I have to instantiate the superclass and use the instance
      Superclass superclass = new Superclass();
      superclass.methodA();
      superclass.methodB();
}

It works both ways, but I want to know which is better to use. 它可以双向工作,但是我想知道哪种更好。 Any of these ways is a bad programming technique? 这些方法中的任何一种都是不好的编程技术吗? I hope you give me answer and arguments. 我希望你能给我答案和论点。

  super.methodA();

  Superclass superclass = new Superclass();
  superclass.methodA();

These two calls of methodA work on different instances, so they are completely different. 这两个对methodA的调用在不同的实例上工作,因此它们是完全不同的。 super.methodA() executes methodA on the current instance. super.methodA()执行methodA在当前实例。 superclass.methodA() executes methodA on a new instance of Superclass which is not related to the current instance. superclass.methodA()在与当前实例无关的新Superclass实例上执行methodA

You would almost always use the first option. 您几乎总是使用第一个选项。 As for the second option, It doesn't make sense to create a new instance, call a method on that instance and then never do anything with that instance again. 至于第二个选项,创建一个新实例,在该实例上调用一个方法,然后再也不对该实例做任何事情是没有意义的。

It works both ways, but I want to know which is better to use. 它可以双向工作,但是我想知道哪种更好。

Well that entirely depends on what you're trying to achieve. 好吧,这完全取决于您要实现的目标。 If you want to create a new, entirely independent instance, do so. 如果创建一个新的,完全独立的实例,请执行此操作。 But it's more common that you want to use the superclass implementation of a method you're overriding on the same instance that the overridden method is currently executing on in which case you would use super.methodA() . 但是,更常见的是,您想在被覆盖的方法当前正在执行的同一实例上使用被覆盖的方法的超类实现,在这种情况下,您将使用super.methodA()

In my experience, super is most commonly used when overriding a method to do some subclass-specific work, call the superclass implementation, then do some more superclass-specific work. 以我的经验,当重写某个方法来执行某些特定于子类的工作,调用超类实现,然后执行一些特定于超类的工作时, super最常使用。 For example: 例如:

@Override public void add(Foo foo) {
    doSomeSubclassSpecificValidation(foo);
    super.add(foo);
    doSomeSubclassSpecificBookKeeping();
}

In other words, even though you're overriding the method, you still want the "normal" behaviour - you just want some extra code to run as well. 换句话说,即使您覆盖了该方法,您仍然想要“正常”行为-您只希望同时运行一些额外的代码。 Or sometimes you want to run the superclass code conditionally, eg only if the input meets a certain criterion. 或者有时您想有条件地运行超类代码,例如,仅当输入满足特定条件时才运行。

It's totally different. 完全不同。

super.methodA() will call methodA() in the left circle, while creating a new superclass and calling that methodA() will first create the right circle, and then call methods from it. super.methodA()将在左侧的圆圈中调用super.methodA() ,同时创建一个新的超类并调用该methodA()将首先创建右侧的圆圈,然后从中调用方法。

With above answers, you must have understood that basically you are calling same method of same class but on two different objects so it all depends as what you are trying to achieve ( On which object you plan to call those methods ). 通过上面的回答,您必须了解,基本上,您在调用同一类的相同方法,但是在两个不同的对象上,所以这都取决于您要实现的目标(您打算在哪个对象上调用这些方法)。 As you know, call to same methods of same class but on different instances are not the same. 如您所知,对相同类的相同方法的调用在不同实例上是不同的。 "super" object is parent of "this" object and that super object was created implicitly when you instantiated Subclass so as per your example code, both are NOT SAME but for simple cases,output might be same. “ super”对象是“ this”对象的父对象,并且在实例化Subclass时隐式创建了该super对象,因此根据您的示例代码,两者都不相同,但在简单情况下,输出可能相同。 Go one more level up and see if it looks different to you from client code ( try writing calling code of Subclass ). 再上一层,看看它是否与客户端代码看起来不同(尝试编写Subclass的调用代码)。

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

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