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

发布于 2024-12-11 10:38:04 字数 463 浏览 5 评论 0 原文

我想知道以下内容在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 {

    }

}

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

如果我很好地解释了自己,请告诉我。

提前致谢!

I am wondering if the following is valid in 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒冷纷飞旳雪 2024-12-18 10:38:04

关于:

public class OuterClass {

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

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

   class InnerClass {

   }

您不需要显式的 OuterClass 标识符,也不需要隐含的 this 。

所以这是不必要的:

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

这在实例方法内部很好:

InnerClass ic = new InnerClass();

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

这不会起作用

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

但是这会很好地工作:

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

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.

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:

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
    }
初见 2024-12-18 10:38:04

内部类的每个实例(除非该类被声明为static)都必须有一个外部类的“连接”实例才能被实例化。

这行不通:

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

但是,这行得通,它不需要 Outer 的实例,因为使用了 static 关键字:

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

更多信息:java内部类

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.

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 static keyword is used:

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

more info: java inner classes

相守太难 2024-12-18 10:38:04

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

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

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();
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文