我不明白为什么我无法访问父类受保护的成员,但我可以访问受子类保护的成员。
如您所见,我可以从Child类访问subchild.x
但我无法从Subchield类访问child.x
。
Isn&#t; t subchild.x
字段也必须无法访问?
package com.company.parentPack;
public class Parent {
protected int x = 3;
}
package com.company.childPack;
public class Child extends Parent {
public int getValueChild(Subchild subchild){
return subchild.x; //works without any problem.
}
public int getValue(Child child){
return child.x;
}
}
package com.company.subchildPack;
public class Subchield extends Child {
public int getValue2(Child child){
return child.x; //Error: java: x has protected access in com.company.parentPack.Parent
}
}