为什么我可以访问和查看共享同一父级的类中的保护区域?我一直认为 protected 只能通过父级或子级本身访问,而不能以任何方式在外部访问。
class Parent {
protected int age;
}
class Sister extends Parent { }
class Brother extends Parent {
public void myMethod(Sister sister) {
//I can access the field of my sister,
// even when it is protected.
sister.age = 18;
// Every protected and public field of sister is visible
// I want to reduce visibility, since most protected fields
// also have public getters and some setters which creates
// too much visibility.
}
}
所以我想它只受到家庭之外的保护。为什么会这样?我怎么能对除了直接父母和孩子以外的家庭成员隐藏一些东西?在我看来,我们似乎缺少访问成员修饰符。除了孩子和父母之外,应该对所有人隐藏类似family
应该和受保护的东西。protected
我不是要求任何人重写 Java,只是注意到。