简体   繁体   English

如何调用内部类构造函数?

[英]How to invoke inner-class constructor?

I've the following classes: 我有以下课程:

package org.gradle;

public class Supcl {

    public class Inner extends Supsupcl {
        public Inner(int a) {
            System.out.println(a);
        }
    }
}

and

package org.gradle;

public class Subcl extends Supcl.Inner {

    public Subcl() {
        Supcl.Inner.super(34); //error: No enclosing instance of type Supcl is 
                               //available due to some 
                               //intermediate constructor invocation

    }
}

And class Supsupcl : 和类Supsupcl

package org.gradle;

public class Supsupcl {}

If I try to replace Supcl.Inner.super(34); 如果我尝试替换Supcl.Inner.super(34); with super(34); super(34); the same error will occur. 同样的错误将会发生。 How do it do? 怎么做?

You are referencing an inner class nested to the instance of Supcl , so the right idiom to initialize the inner class is: 您正在引用嵌套到Supcl实例的内部类,因此初始化内部类的正确习惯是:

new Supcl().new Inner(int i)
(assuming Supcl has a 0-argument constructor or the default constructor)

You cannot invoke super outside the constructor, so you'll have to invoke it as the first line of your Inner(int i) constructor code. 您不能在构造函数之外调用super ,因此您必须在Inner(int i)构造函数代码的第一行中调用它。

■ From inside the outer class instance code, use the inner class name in the normal way: ■从外部类实例代码内部,以常规方式使用内部类名称:

MyInner mi = new MyInner();

■ From outside the outer class instance code (including static method code within the outer class), the inner class name must now include the outer class's name: ■从外部类实例代码外部(包括外部类内部的静态方法代码)开始,内部类名称现在必须包括外部类的名称:

   MyOuter.MyInner

To instantiate it, you must use a reference to the outer class: 要实例化它,必须使用对外部类的引用:

  new MyOuter().new MyInner(); or outerObjRef.new MyInner();

if you already have an instance of the outer class. 如果您已经有外部类的实例。

Since non-static inner classes have a special relationship with their outer class you always need an instance of the outer class to create an instance of the inner class. 由于非静态内部类与其外部类具有特殊关系,因此您始终需要外部类的实例来创建内部类的实例。

In your case, where a non-inner class extends an inner class, you'll need to either create an instance of the outer class or pass it: 对于非内部类扩展内部类的情况,您将需要创建外部类的实例或将其传递:

public class Subcl extends Supcl.Inner {
  public Subcl() {
    //assuming there is a no-argument constructor Supcl()
    //this only works in one statement so the call to super is still 
    //part of the first statment in this constructor
    new Supcl().super(34); //will call Inner(34)
  }

  public Subcl( Supcl sup) {
    sup.super(34);  //will call Inner(34)
  }
}

Note that you'd have to do this even if Inner had a no-argument constructor, since you need to somehow get the instance of Supcl and the compiler can't deduce this. 请注意,即使Inner具有无参数构造函数,您也必须这样做,因为您需要以某种方式获取Supcl的实例,并且编译器无法推断出这一点。

Finally a word of advise (although it is primarily my opinion): as you can see the syntax for inner classes is not always straight forward and it might be hard to grasp what's going on in some cases, thus I'd suggest using inner classes only in simple cases and only if you really need them. 最后,提个建议(尽管这主要是我的意见):正如您所看到的,内部类的语法并不总是直截了当的,在某些情况下可能很难理解所发生的事情,因此我建议使用内部类仅在简单情况下,并且仅在您确实需要它们时。 It will make your life a lot easier :) 它将使您的生活更加轻松:)

Make the inner static 使内部static

public static class InnerClass {

} 

and access it like 并像访问

ParentClass.InnerClass innerClass = new ParentClass.InnerClass(args); 

where args is your custom parameters if you have any args是您的自定义参数(如果有)

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

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