简体   繁体   English

如何使用super关键字调用innerClass之外的方法

[英]How to call method outside the innerClass using super keyword

How do I call a method outside the innerClass?如何调用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?我做了 extends 方法,但它告诉我即使我做了 extends 方法 Attributes 也未定义,是因为我不能在类中扩展类吗?

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

super.x() is a way to invoke methods from your parent class. super.x()是一种从父类调用方法的方法

Attributes is not a method . Attributes不是方法 In fact, that Attributes() {} at the very end of your paste isn't a method either;实际上,粘贴末尾的Attributes() {}也不是方法; 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.如果你想创建一个新的 Attributes 对象,你可以使用new关键字,而super是正确的——你不能使用super来调用这样的东西。 But you don't have to - just new Attributes();但你不必 - 只需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.会这样做,因为 super只有一个目的- 即区分对您自己的类中的方法的调用与您覆盖的版本。 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.请注意在上面的示例中如何没有super. and yet that will work fine, and the call to hello() would print "From parent" .但这会正常工作,并且对hello()的调用将打印"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.此外, super.x()仅在类本身中有效,而在其任何内部类中无效,幸运的是,这对您的情况无关紧要。

NB: super is used for three mostly to completely unrelated java features.注意: super用于三个大部分到完全不相关的 java 特性。 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:最后一个可能是您想要的,但这仅在构造函数本身中有效,并且main不是构造函数:

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();如果你没有这样做,java 就像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.您可以使用super(argsHere)显式选择要调用的父类的构造函数之一。

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

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