标签:
书本上的例子
改编后的。
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 }
可以顺利运行。
标签:
原文地址:http://www.cnblogs.com/01picker/p/4316578.html