public 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 {
    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() {}
  }
  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());//为什么a2.receiveD()可以?
  }
} ///:~

解决方案 »

  1.   

    为什么a2.receiveD()可以? 
    -------------。
    到底是可以还是不可以?
      

  2.   

    为什么不可以?
    a2是类A的一个对象,而类A中有公共方法receiveD,a2调用它a2.receiveD()
    这很正常啊
      

  3.   

    JAVA社区联盟2号群群号69726300,我是秋风
      

  4.   

    A.DImp2 di2 = a.getD(); 这个不可以,其它都可以吧,D不能转型成DImp2吧