-1
爲什麼這段代碼運行良好?爲什麼受保護的成員是通過放置在不同包中的類的子類的子類訪問的
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
但如何才能的subclass(C)
subclass(Child)
可以訪問父類的保護成員。
的[什麼是受保護的修飾符是什麼意思?]可能的複製(http://stackoverflow.com/questions/8637781/what-does-the-protected-modifier-mean) – khelwood
另一個鏈接可以幫助你理解java中訪問修飾符的區別。 http://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-and-private – dnapierata
[在Java中,默認,公共,保護和私人之間的區別](http ://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-and-private) – dnapierata