这两个有什么不同吗?frm.addWindowListener(this)与addWindowListener(frm) frm是frame的对像.谢谢啊

来源:百度知道 编辑:UC知道 时间:2024/05/29 05:28:51
什么情况不分别用那个?
什么属于匿名类?

接一楼的:
这两个最大的不同就在于被监听的对象和添加的窗口监听器对象不一样。
frm.addWindowListener(this)//自己监听frm。
addWindowListener(frm)//frm监听自己。
匿名类就是没有取名字的类。
一般类定义:TestClass test = new TestClass();
而像下面这样:
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
new WindowAdapter()就没取名字。(注意WindowAdapter可以换成接口,但是里面的方法都要实现了)

this 代表当前对象.
打个比方吧:你在class A 中写frm.addWindowListener(this),那么this就是 class A.

匿名类 例:
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

其中new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
就是匿名类..