我有以下代码.我想掌握使用它创建内部类对象的外部类对象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)
编辑:嗯,你们中的一些人建议通过添加一个方法来修改内部类: …