Anonymous Inner Class (匿名内部类)

匿名类本身就是通过继承类或者接口来实现的。但是不能再显式的extends 或者implements了。
举个例子:
JFrame frame = new JFrame();
我们为frame添加窗口事件可以采用如下两种方式:

Java code
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> 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
  
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> frame.addWindowListener( new WindowAdapter() {});


上面这个匿名类就是继承了WindowAdapter这个抽象类。

你可能感兴趣的:(inner class)