日期:2014-05-20  浏览次数:20750 次

关于interface的问题
[code=Java][/code]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()可以?
  }
} ///:~

------解决方案--------------------
为什么不可以?
a2是类A的一个对象,而类A中有公共方法receiveD,a2调用它a2.receiveD()
这很正常啊
------解决方案--------------------
A.DImp2 di2 = a.getD(); 这个不可以,其它都可以吧,D不能转型成DImp2吧
------解决方案--------------------
探讨
为什么不可以?
a2是类A的一个对象,而类A中有公共方法receiveD,a2调用它a2.receiveD()
这很正常啊