- Java code
class A { protected int i = 1;}class B extends A { protected int i = 2;}public class C extends B { int i = 3; public static void main(String[] args) { new C().test(); } void test() { System.out.println(i); System.out.println(super.i); //请问如何访问 A 的 i ? }}
------解决方案--------------------
没办法直接调用的,可以通过其他方式来获取。例如:
Class c = this.getClass().getSuperclass().getSuperclass();
Field f = c.getDeclaredField("i");
System.out.println(f.getInt(c.newInstance()));