java 从当前外部类对象实例化内部类对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7856808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:42:43  来源:igfitidea点击:

Instantiate inner class object from current outer class object

javainner-classes

提问by romeroqj

I am wondering if the following is valid in Java:

我想知道以下内容在 Java 中是否有效:

class OuterClass {

    OuterClass(param1, param2) {
        ...some initialization code...
    }

    void do {
       // Here is where the doubt lays
       OuterClass.InnerClass ic = this.new InnerClass();
    }

    class InnerClass {

    }

}

Basically what I am trying to achieve here is to instantiate an inner class object from the current instance of the outer class, not a new instance, the current one. I believe this comes handy is when the constructor of the outer class is not empty (takes parameters) and we don't know what pass to them (they can't be null since some might be assigned to a class variable that is accessed by the inner class object).

基本上我在这里想要实现的是从外部类的当前实例实例化一个内部类对象,而不是一个新实例,即当前实例。我相信当外部类的构造函数不为空(带参数)并且我们不知道传递给它们的内容时,这很方便(它们不能为空,因为有些可能被分配给访问的类变量)内部类对象)。

Let me know if I explained myself well.

让我知道我是否很好地解释了自己。

Thanks in advance!

提前致谢!

回答by Hovercraft Full Of Eels

Regarding:

关于:

public class OuterClass {

   OuterClass() {
       // ...some initialization code...
   }

   void doSomething() {
      OuterClass.InnerClass ic = this.new InnerClass();
   }

   class InnerClass {

   }

You don't need the explicit OuterClass identifier nor the this as they're implied.

您不需要显式的 OuterClass 标识符,也不需要它们暗示的 this 。

So this is unnecessary:

所以这是不必要的:

OuterClass.InnerClass ic = this.new InnerClass();

And this is fine inside of an instance method:

这在实例方法中很好:

InnerClass ic = new InnerClass();

Things get dicier though if you're creating an object of InnerClass in a static method such as main that is held inside of OuterClass. There you'll need to be more explicit:

但是,如果您在静态方法中创建 InnerClass 的对象,例如保存在 OuterClass 内部的 main,事情会变得更加棘手。在那里你需要更明确:

This won't work

这行不通

public class OuterClass {
    public static void main(String[] args) {
       InnerClass otherInnerVar = new InnerClass(); // won't work
    }

But this will work fine:

但这会正常工作:

public class OuterClass {
    public static void main(String[] args) {
       InnerClass otherInnerVar2 = new OuterClass().new InnerClass(); // will  work
    }

回答by amit

Every instance of an inner class, unless the Class is declared as static, must have a 'connected' instance of an outer class, in order to be instantiated.

内部类的每个实例,除非 Class 被声明为static,否则必须有一个外部类的“连接”实例,才能被实例化。

This won't work:

这行不通:

public class Outer {
    public class Inner { 
    }
    public static void main(String[] args) {
        Inner inner = new Inner(); //compilation error
    }
}

However, this will work, it doesn't need an instance of Outer, since the statickeyword is used:

但是,这会起作用,它不需要 的实例Outer,因为使用了static关键字:

public class Outer {
    public static class Inner { 
    }
    public static void main(String[] args) {
        Inner inner = new Inner(); 
    }
}

more info: java inner classes

更多信息:java内部类

回答by Abhay Pore

Above is the example for creating Inner class object inside outer class and outside outer class:

以上是在外部类和外部类中创建内部类对象的示例:

public class OuterClass {

public class InnerClass{

    public void myMethod(){
        System.out.println("inside inner class");
    }
}

public void myMethod(){
    System.out.println("outer class method");
    InnerClass class1 = new InnerClass();
    class1.myMethod();
}

public static void main(String[] args){
    //OuterClass.InnerClass class1 = new OuterClass().i
    OuterClass outerClassObj = new OuterClass();
    OuterClass.InnerClass innerClassObj = outerClassObj.new InnerClass();
    innerClassObj.myMethod();
}
}