从内部类引用外部类实例

时间:2014-01-21 08:57:15

标签: java

我想将在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();
  }
}

1 个答案:

答案 0 :(得分:5)

试试这个(好...... MPGUI.this实际上):

public class MPGui {
        public MPGui() {
                //initialize GUI
        }

        public class ExecuteListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {

                        Executor execu = new Executor(MLA, MPGUI.this);
                        execu.execute();

                }
        }

        public static void main(String[] args) {
                MPGui a = new MPGui();
        }
}