众所周知,私有字段不会在类之间继承。但是,我很好奇内部静态类的情况。
考虑以下代码:
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”结构访问呢?