适用于一个具体类只想实现接口中部分抽象方法这种情况
让一个具体类只实现接口中部分的抽象方法
(1)让一个抽象类平庸实(大括号里什么都不写)目标接口
(2)让这个具体类继承抽象类,然后覆盖自己关心的方法
public abstract class WindowAdapter implements WindowListener {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}
public class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
// 退出JVM
System.exit(0);
}
}
public class MyFrame extends Frame {
public static void main(String[] args) {
Frame f = new Frame("我的窗口程序");
f.setSize(600, 400);
f.setLocation(100, 100);
// 把监听器和窗口进行绑定
f.addWindowListener(new MyWindowListener());
f.setVisible(true);
}
}