码迷,mamicode.com
首页 > 编程语言 > 详细

《thinking in java》 接口与内部类

时间:2015-03-05 20:47:07      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

书本上的例子

 

技术分享

 

改编后的。

 

 1 package test2;
 2 
 3 class A{
 4     interface B{void f();}
 5     public class BImp implements B{public void f(){}}
 6     private class BImp2 implements B{public void f(){}}
 7     
 8     public interface C{void f();}
 9     class CImp implements C{public void f(){}}
10     private class CImp2 implements C{public void f(){}}
11     
12     private interface D{void f();}
13     private class DImp implements D{public void f(){}}
14     public class DImp2 implements D{public void f(){}}
15     
16     public D getD(){return new DImp2();}
17     private D dRef;
18     public void receiveD(D d){dRef=d;dRef.f();}    
19     
20 }
21 interface E{
22     interface G{void f();}
23     
24     public interface H{void f();}
25     void g();
26 }
27 public class NestingInterfaces {
28 
29     public class BImp implements A.B{public void f(){}}
30     class CImp implements A.C{public void f(){}}
31     
32 //    The type A.D is not visible
33 //    class DImp implements A.D{public void f(){}}
34     
35     class EImp implements E{public void g(){}}
36     class EGimp implements E.G{public void f(){}}
37     
38     class EImp2 implements E{
39         public void g(){}
40         class EG implements E.G{public void f(){}}
41     }
42     
43     public static void main(String[] args) {
44         A a=new A();
45         
46         //The type A.D is not visible
47 //        A.D ad=a.getD();
48         
49         //Type mismatch: cannot convert from A.D to A.DImp2
50 //        A.DImp2 di2=a.getD();
51         
52         A.DImp2 di2=(A.DImp2)a.getD();
53         System.out.println(di2.getClass().getName());//test2.A$DImp2
54          
55         //The type A.D is not visible
56 //        a.getD().f();
57         
58         ((A.DImp2)a.getD()).f();
59         
60         A a2=new A();
61         a2.receiveD(a.getD());
62     }
63     
64 }

可以顺利运行。

《thinking in java》 接口与内部类

标签:

原文地址:http://www.cnblogs.com/01picker/p/4316578.html

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