What is the meaning of “this” in Java?

后端 未结 21 2942
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

21条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 06:10

    In Swing its fairly common to write a class that implements ActionListener and add the current instance (ie 'this') as an ActionListener for components.

    public class MyDialog extends JDialog implements ActionListener
    {
        public MyDialog()
        {
            JButton myButton = new JButton("Hello");
            myButton.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent evt)
        {
            System.out.println("Hurdy Gurdy!");
        }
    
    }
    

提交回复
热议问题