如何使用super关键字在内部类外部调用方法

huangapple go评论75阅读模式
英文:

How to call method outside the innerClass using super keyword

问题

以下是您提供的代码的翻译部分:

如何在innerClass外部调用一个方法?我使用了继承方法,但是它告诉我方法Attributes未定义,尽管我已经进行了继承,是因为我不能在一个类内部继承另一个类吗?

这是我的代码:

package mainClass;

import java.util.Random;
import mainClass.Characters;
import mainClass.Main;

class Characters {
  
  static String name = "";
  static int attack = 0;
  static int maxAttack = 0;
  static int hp = 0;
  static int def = 0;
  
  static class Selena extends Attributes {
    
    private static void main() {
      
      super.Attributes();
      //我继承了Attributes,但为什么它不能识别Attributes类呢?
      
    }
    
    public static void Scratch() {
      
      System.out.println(" 使用Scratch!");
      
    }
    
  }
  
  
  static class Attributes {
    
    Attributes() {
      
    }
    
  }
  
}
英文:

How do I call a method outside the innerClass? I did the extends method but it told me that the method Attributes is undefined even though I did extends, is it because I can't extend a class inside a class?

Here's my code:

package mainClass;

import java.util.Random;
import mainClass.Characters;
import mainClass.Main;

class Characters {
  
  static String name = "";
  static int attack = 0;
  static int maxAttack = 0;
  static int hp = 0;
  static int def = 0;
  
  static class Selena extends Attributes {
    
    private static void main() {
      
      super.Attributes();
      //I extend Attributes but why does he not recognize the class Attributes
      
    }
    
    public static void Scratch() {
      
      System.out.println(" uses Scratch!");
      
    }
    
  }
  
  
  static class Attributes {
    
    Attributes() {
      
      
      
    }
    
  }
  
}

答案1

得分: 1

super.x() 是一种从父类中调用方法的方式。

Attributes 不是一个方法。事实上,在你粘贴的最后,那个 Attributes() {} 不是一个方法,而是一个构造函数。

如果你想创建一个新的 Attributes 对象,你需要使用 new 关键字,而 super 则不适用 - 你不能使用 super 来调用这样的东西。但是你也不需要这么做 - 只需要 new Attributes(); 就可以了,因为 super 有且仅有一个目的 - 区分对自己类中的方法的调用与对你重写的版本的调用。因此:

class Parent {
    void hello() {
        System.out.println("From parent");
    }
}

class Child extends Parent {
    void foo() {
        hello();
    }
}

请注意,在上面的示例中没有 super.,但这也可以正常工作,对 hello() 的调用将打印出 "From parent"。但是:

class Child extends Parent {
    void hello() {
        // this overrides
        System.out.println("From child");
    }

    void foo() {
        hello(); // prints 'from child'
        super.hello(); // prints 'from parent'
    }
}

此外,super.x() 仅在类本身中有效,而不在任何内部类中有效,幸运的是,这对你的情况没有影响。

注意:super 在三个几乎完全不相关的 Java 特性中被使用。泛型逆变边界(这与你的操作完全无关,所以我不会进一步深入讨论它),调用父类方法的实现,以及选择首先运行父类构造函数。最后一个可能是你想要的,但这仅在构造函数本身中有效,而 main 不是构造函数:

class MyAttrs extends Attributes {
    public MyAttrs(String x) {
        super(x);
    }
}

class Attributes {
    public Attributes(String x) {
        // ....
    }
}

这两个看起来像方法的东西实际上是构造函数;在任何构造函数中,你必须首先调用一个父类构造函数或者自己的构造函数。如果你未能这样做,Java 将会像在方法顶部放置了 super(); 一样对待。你可以使用 super(argsHere) 来明确选择要调用的父类构造函数之一。

英文:

super.x() is a way to invoke methods from your parent class.

Attributes is not a method. In fact, that Attributes() {} at the very end of your paste isn't a method either; it's a constructor.

If you want to make a new Attributes object, you use the new keyword, and super is right out - you can't use super to invoke such things. But you don't have to - just new Attributes(); will do it, because super has only one purpose - that is to differentiate the call to the method in your own class vs. the version you overrode. Thus:

class Parent {
    void hello() {
        System.out.println("From parent");
    }
}

class Child extends Parent {
    void foo() {
        hello();
    }
}

Note how in the above example there is no super. and yet that will work fine, and the call to hello() would print "From parent". But:

class Child extends Parent {
    void hello() {
        // this overrides
        System.out.println("From child");
    }

    void foo() {
        hello(); // prints 'from child'
        super.hello(); // prints 'from parent'
    }
}

Furthermore, super.x() is only valid in the class itself and not any inner class thereof, which fortunately doesn't matter for your case.

NB: super is used for three mostly to completely unrelated java features. Generics contravariant bounds (this is completely unrelated to what you're doing, so I won't delve into it further), invoking the parent class implementation of methods, and picking which constructor of parent to run first. That last one may be what you want, but that is only valid in constructors themselves, and main is not a constructor:

class MyAttrs extends Attributes {
    public MyAttrs(String x) {
        super(x);
    }
}

class Attributes {
    public Attributes(String x) {
        // ....
    }
}

Both of the looks-like-a-method thingies are constructors; You MUST invoke exactly one of your parent constructors, OR one of your own, as first thing in any constructor. If you fail to do so, java acts as if super(); is at the top of your method. You can use super(argsHere) to explicitly choose one of the constructors of your parent class to invoke.

huangapple
  • 本文由 发表于 2020年10月27日 18:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64552745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定