【发布时间】:2017-06-04 11:53:54
【问题描述】:
为什么这段代码运行良好??
package certification;
public class Parent {
protected int x = 9; // protected access
protected void z(){System.out.println(5);}
}
package other;
import certification.Parent;
class C extends Parent{}
public class Child extends C {
public void testIt() {
System.out.println("x is " + x);
this.z();
}
}
import other.Child;
class Test extends Child{
public static void main(String args[]){
new Child().testIt();
}
}
这给出了输出:
x 是 9
5
但是subclass(C) 的subclass(Child) 怎么能访问Parent 类的受保护成员。
【问题讨论】:
-
另一个链接,可以帮助您了解 java 中访问修饰符之间的区别。 stackoverflow.com/questions/215497/…