码迷,mamicode.com
首页 > 其他好文 > 详细

匿名内部类

时间:2015-08-02 21:24:29      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

匿名内部类

标签:

原文地址:http://www.cnblogs.com/erhai/p/4696726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!