Posted on 2008-10-10 09:30
林光炎 阅读(2701)
评论(0) 编辑 收藏
匿名类本身就是通过继承类或者接口来实现的。但是不能再显式的extends 或者implements了。
举个例子:
JFrame frame = new JFrame();
我们为frame添加窗口事件可以采用如下两种方式:
- Java code
-
frame.addWindowListener(new WindowListener() {
public void windowOpened(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowClosing(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowClosed(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowIconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowDeiconified(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowActivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void windowDeactivated(WindowEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
这个匿名类就是实现了接口WindowListener
或者:
- Java code
-
frame.addWindowListener(new WindowAdapter() {});
上面这个匿名类就是继承了WindowAdapter这个抽象类。