匿名内部类

How

new 父类或接口(){继承父类或者实现接口所需的内容};      //注意理解这里的分号

补全以下内容

 1 interface Inter
 2 {
 3     void method();
 4 }
 5 class Test
 6 {
 7     //补全代码,要求通过内部类
 8     
 9 
10 }
11 class NiMingNeiBu 
12 {
13     public static void main(String[] args) 
14     {
15         Test.function().method();
16     }
17 }

分析

 1 interface Inter
 2 {
 3     void method();
 4 }
 5 class Test
 6 {
 7     //静态 返回Inter
 8     static Inter function(){
 9         //返回Inter,而Inter是一个接口,不能被实例-》new Inter(){实现接口}<=>实现了接口Inter的匿名类
10         return new Inter()
11         {
12             public void method(){
13                 System.out.println("欧了");
14             }
15         };
16     }
17 
18 }
19 class NiMingNeiBu 
20 {
21     public static void main(String[] args) 
22     {
23         /*分析:
24         1:Test.function(),说明在Test内部定义了一个静态方法function()
25         2:.method()说明function返回的是一个实现了Inter的对象
26         */
27         Test.function().method();
28     }
29 }
原文地址:https://www.cnblogs.com/erhai/p/4696726.html