Java 内部接口

在类AppearTime 中创建接口IClear,并创建接口内部类MyClear,在main主方法中初始化MyClear

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class AppearTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyClear clear=new AppearTime().new MyClear();
clear.clear();
}
//内部接口
interface IClear{
public void clear();
}
//接口实现类
class MyClear implements IClear{
@Override
public void clear() {
// TODO Auto-generated method stub
System.out.println("do clear...");
}
}
}

内部接口类在调用时不能直接调用

1
MyClear clear=new MyClear; // 会报错

应该写成

1
MyClear clear=new AppearTime().new MyClear();