相关疑难解决方法(0)

从内部类对象获取外部类对象

我有以下代码.我想掌握使用它创建内部类对象的外部类对象inner.我该怎么做?

public class OuterClass {

    public class InnerClass {
        private String name = "Peakit";
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        InnerClass inner = outer.new InnerClass();
       // How to get the same outer object which created the inner object back?
        OuterClass anotherOuter = ?? ;

        if(anotherOuter == outer) {
             System.out.println("Was able to reach out to the outer object via inner !!");
        } else {
             System.out.println("No luck :-( ");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,你们中的一些人建议通过添加一个方法来修改内部类: …

java inner-classes

228
推荐指数
3
解决办法
14万
查看次数

标签 统计

inner-classes ×1

java ×1