近来又翻起thinking in java 3rd这本书,看到了在类中嵌套接口这一部分
//A.java
class A {  private interface D {
    void f();
  }
  
  public class DImp2 implements D {
    public void f() {}
  }
  
  public D getD() { return new DImp2(); }
  
  private D dRef;
  
  public void receiveD(D d) {
    dRef = d;
    dRef.f();
  }   public static void main(String[] args) {
    A a = new A();
    // Can't access A.D:
    // A.D ad = a.getD();
    // Doesn't return anything but A.D:
    // A.DImp2 di2 = a.getD();
    // 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());
  }
} B.E在分析这段代码的时候写道:
You are not allowed to mention the fact that it implements the 
private interface, so implementing a private interface is a way 
to force the definition of the methods in that interface without
adding any type information (that is, without allowing any 
upcasting).
shhgs是这么翻译的:你不能透露任何"它实现了一个private 接口"信息,
因此实现private 接口就成了一种"强制你去定义那个接口的方法,但又不
让你添加任何类型信息(也就是说不允许向上转型)"的手段了。大家试着把文件改成:
//A.java
class A {  private interface D {
   int i = 5;
    void f();
  }
  
  public class DImp2 implements D {
    public void f() {}
  }
  
  public D getD() { return new DImp2(); }
  
  private D dRef;
  
  public void receiveD(D d) {
    dRef = d;
    dRef.f();
  }   public static void main(String[] args) {
    A a = new A();
    // Can't access A.D:
    A.D ad = a.getD();  //向上转型
    // Doesn't return anything but A.D:
    // A.DImp2 di2 = a.getD();
    // Cannot access a member of the interface:
    System.out.println(a.getD().i);  //访问接口的成员
    a.getD().f();  //访问接口的成员
    // Only another A can do anything with getD():
    A a2 = new A();
    a2.receiveD(a.getD());
  }
} 编译通过。怎么回事,有读过thinking in java 的明白人指点一下??

解决方案 »

  1.   

    class A {                                         
                                                      
      private interface D 
      {                           
        void f();                                     
      }                                               
       
      private int i = 5;
                                                     
      public class DImp2 implements D 
      {               
        public void f() {}                            
      }                                               
                                                      
      public D getD() { return new DImp2(); }         
                                                      
      private D dRef;                                 
                                                      
      public void receiveD(D d) 
      {                     
        dRef = d;                                     
        dRef.f();                                     
      }                                               
       // main嵌套在类A中,因此它属于类A的friend 方法,可以访问到类A的private属性.
                                                   
       public static void main(String[] args) {       
         A a = new A();                                
         // Can't access A.D:                          
          A.D ad = a.getD();                         
         // Doesn't return anything but A.D:           
         //A.DImp2 di2 = a.getD();                    
         // Cannot access a member of the interface:   
         a.getD().f();  
        
         a.i = 6;// 可以访问
                                     
         // Only another A can do anything with getD():
         A a2 = new A();                               
         a2.receiveD(a.getD());                        
      }                                               
    }   以上编译通过.
    如果你添加下面代码到A中,编译将不会通过.public class B 
    {
    public static void main(String[] args) {       
         A a = new A();                                
         // Can't access A.D:                          
          A.D ad = a.getD();                         
         // Doesn't return anything but A.D:           
         //A.DImp2 di2 = a.getD();                    
         // Cannot access a member of the interface:   
         a.getD().f();  
        
         a.i = 6;
                                     
         // Only another A can do anything with getD():
         A a2 = new A();                               
         a2.receiveD(a.getD());                        
       }  
    }
      

  2.   

    TO:arthurnew():
    不好意思,我好像断章取义了。
    楼主为什么这么说?
    To:yuzl32(Hello!有酒醉)
    friend 方法?java有这样的说法吗?