Java 子类的访问控制
Access control from Java subclass
我想我在 java 官方文档中发现了一个矛盾:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
一方面,它说:
" If the subclass is in the same package as its parent, it also
inherits the package-private members of the parent."
之后,它说:
A subclass does not inherit the private members of its parent class.
这不是自相矛盾的说法吗?
我认为第二种说法是正确的。私有字段只能在它定义的 class 中访问(除非我们在 class 中定义了私有或受保护的获取访问器)。谢谢。
package-private
与 private 不同,尽管名称 "private" 在两者中都有。
package-private
是当您没有任何成员限定符时
public class Bar{
public int foo; // public
int foo1; // package private
private int foo2; //private
}
我想我在 java 官方文档中发现了一个矛盾: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
一方面,它说:
" If the subclass is in the same package as its parent, it also inherits the package-private members of the parent."
之后,它说:
A subclass does not inherit the private members of its parent class.
这不是自相矛盾的说法吗? 我认为第二种说法是正确的。私有字段只能在它定义的 class 中访问(除非我们在 class 中定义了私有或受保护的获取访问器)。谢谢。
package-private
与 private 不同,尽管名称 "private" 在两者中都有。
package-private
是当您没有任何成员限定符时
public class Bar{
public int foo; // public
int foo1; // package private
private int foo2; //private
}