问一个关于this的问题
import java.awt.*;import java.awt.event.*;
import java.io.Serializable;
class MyApp implements ActionListener, Serializable
{
BigObjectThatShouldNotBeSerializedWithAButton bigOne;
Button aButton = new Button();
MyApp()
{
// Oops, now aButton has a listener with a reference
// to bigOne!
aButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Hello There");
}
}
jdk文档的这段实例代码,在添加事件监听器里,用了this关键字,这是则么传递的,是this的哪个用法,是调用构造函数还是作为参数传递?
----------------解决方案--------------------------------------------------------
这个this实际上是代表默认的监听器, 注意你的类已经implements ActionListener. 所以this代表..
----------------解决方案--------------------------------------------------------
因为你继承了ActionListener接口,所以你自己构造的类本身就是你要用的监听器类,this其实代表了new MyApp()
----------------解决方案--------------------------------------------------------