众所周知,私有字段不会在类之间继承。什么让我感兴趣,它是如何工作的内部静态类。 考虑下面的代码:访问超类的私有字段
public class Main {
public static void main(String[] args) {
new B();
}
private static class A {
private int a = 10;
private void foo() {
System.out.println("A.foo");
}
}
private static class B extends A {
{
// foo(); // compile-time error
super.foo(); // ok
// System.out.println(a); // compile-time error
System.out.println(super.a); // ok
}
}
}
能否请你解释它是如何可以访问其他内部类的私有字段?如果它是合法的,为什么只有通过“super.XXX”构造才有可能?
我认为修饰符只与外部类有关。内部类都崩溃了...... – 2014-09-23 09:24:42