看到接口嵌套了,书中有一个例子,如下:
 //:   c08:NestingInterfaces.java   
  //   From   'Thinking   in   Java,   2nd   ed.'   by   Bruce   Eckel   
  //   www.BruceEckel.com.   See   copyright   notice   in   CopyRight.txt.   
    
  class   A   {   
      interface   B   {   
          void   f();   
      }   
      public   class   BImp   implements   B   {   
          public   void   f()   {}   
      }   
      private   class   BImp2   implements   B   {   
          public   void   f()   {}   
      }   
      public   interface   C   {       //为什么可以为public?   
          void   f();   
      }   
      class   CImp   implements   C   {   
          public   void   f()   {}   
      }   
      private   class   CImp2   implements   C   {   
        public   void   f()   {}   
      }   
      private   interface   D   {   
          void   f();   
      }   
      private   class   DImp   implements   D   {   
          public   void   f()   {}   
      }   
      public   class   DImp2   implements   D   {   
          public   void   f()   {   
          System.out.println("jyh");   
          }   
      }   
      public   D   getD()   {   return   new   DImp2();   }   
        
      private   D   dRef;   
        
      public   void   receiveD(D   d)   {     
          dRef   =   d;     
          dRef.f();   
      }   
        
  }   
    
  interface   E   {   
      interface   G   {   
          void   f();   
      }   
      //   Redundant   "public":   
      public   interface   H   {   
          void   f();   
      }   
      void   g();   
      //   Cannot   be   private   within   an   interface:   
      //!   private   interface   I   {}   
  }   
    
  public   class   NestingInterfaces   {   
      public   class   BImp   implements   A.B   {   
          public   void   f()   {}   
      }   
      class   CImp   implements   A.C   {   
          public   void   f()   {}   
      }   
      //   Cannot   implement   a   private   interface   except   
      //   within   that   interface's   defining   class:   
      //!   class   DImp   implements   A.D   {   
      //!     public   void   f()   {}   
      //!   }   
      class   EImp   implements   E   {   
          public   void   g()   {}   
      }   
      class   EGImp   implements   E.G   {   
          public   void   f()   {}   
      }   
      class   EImp2   implements   E   {   
          public   void   g()   {}   
          class   EG   implements   E.G   {   
              public   void   f()   {}   
          }   
      }   
      public   static   void   main(String[]   args)   {   
          A   a   =   new   A();   
          A.DImp2   aa   =   a.new   DImp2();   
          aa.f();   
            
          //   Can't   access   A.D:   
          //!   A.D   ad   =   a.getD();   
          //   Doesn't   return   anything   but   A.D:   
          //!   A.DImp2   di2   =   a.getD();     //   incompatible   types   
          //   Cannot   access   a   member   of   the   interface:   
          //!   a.getD().f();   
          //   Only   another   A   can   do   anything   with   getD():   
          A   a2   =   new   A();   
          a2.receiveD(a.getD());   
      }   
  }   ///:~     
我有如下几个问题,想请教:
1)关于A.D   ad   =   a.getD(); 不可以,是不是因为由于A.D 是private的,所以在类NestingInterfaces根本不知道有A.D的存在,所以也就无法定义A.D 这种接口的对象了?
2)A.DImp2 是一个实现了A.D的一个类,a.getD()创造了一个DImp2的对象,并把它向上转型为其基类接口D,并返回,是不是因为D不会自动向下转型为DImp2, 所以这是不对的?
不知道我这两个问题理解的对不?
3)最后一个问题是我不理解为什么a.getD().f() 不行,而a2.receiveD(a.getD())却可以,a.getD()返回的虽然是一个D, 但它是一个DImp2的对象,为什么不可以调用DImp2中的f()?
 谢谢 

解决方案 »

  1.   

    (1)正确,接口D是private的,你在类NestingInterfaces中无法访问(2)注意a.getD()方法
    public  D  getD()  {  return  new  DImp2();  }  
    它return的是DImp2(),但返回类型却是D,所以A.DImp2  di2  =  a.getD(); 是不行的,等式右边是D类型,左边是DImp2类型,而Dimp2实现了D接口,需要强制类型转换:A.DImp2  di2  = (A.DImp2)a.getD(); 这样就可以了(3)还是因为接口D是私有的,你通过public方法getD()返回私有对象是可以的,所以a.getD()没问题,a2.receiveD(a.getD())也没问题,但你不能调用私有接口的方法f(),没有这个权限
      

  2.   


    明白点了,是否可以这样理解,D只有在A类内部是可见的,所以当a.getD()产生一个D对象时,由于是在A类的外边,根本就不知道D是什么,所以也就没法调用D的f()方法了,对不?