我有以下代码.我想掌握使用它创建内部类对象的外部类对象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)
编辑:嗯,你们中的一些人建议通过添加一个方法来修改内部类: …
我们如何访问外部类这个实例:例如in
Class A {
Class B {
this.helloB();
(A's this).hello()
}
}
Run Code Online (Sandbox Code Playgroud)
我们如何在Java中访问A的这个实例
我想将在main方法中创建的确切实例传递给具有MPGui作为参数的新Executor.这可能吗?
public class MPGui {
public MPGui() {
//initialize GUI
}
public class ExecuteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Executor execu = new Executor(MLA, /*the MPGUI() instance */);
execu.execute();
}
}
public static void main(String[] args) {
MPGui a = new MPGui();
}
}
Run Code Online (Sandbox Code Playgroud)