Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements接口

 

匿名类本身就是通过继承类或者接口来实现的。但是不能再显式的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这个抽象类。

你可能感兴趣的:(Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements接口)