匿名的内部类是没有名字的内部类。 可以(extends继承) 其它类,也可以实现接口。
匿名内部类要实现或者继承类是可以的,当然匿名内部类不能用implements,extends关键字来实现或继承其它类的
Java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package other;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class Test {
JFrame frame= new JFrame();
public Test() {
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
或者:
frame.addWindowListener( new WindowAdapter() {});
上面这个匿名类就是继承了WindowAdapter这个抽象类。
|
描述:Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)吗? 匿名类本身就是通过继承类或者接口来实现的。但是不能再显式的extends 或者implements了。