class A {
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 class NestingInterfaces {
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());
}
}
说明:
上面用“//”注释掉的代码都是编译或者运行时要出错的代码
上面class A文件和class NestingInterfaces文件都是写在同一个编译文件里的
问题:
问题1:怎样理解这种用private修饰的嵌套接口?
问题2:正如上面两个内部类,一个是用private修饰的一个是public修饰的,然后都实现了这个私有话的接口,那么怎样来理解这两个不同修饰符的内部类实现都这个私有化接口后对自身的影响?
问题3:为什么getD()方法的返回值要由另一个A对象a2的receiveD()方法处理,a对象不行吗?