为什么 protected 对子类的不同包中的其他类私有(private)。但它仍然在父类(super class)的同一个包中受到保护。
package a;
class A
{
protected a;
}
package b;
class B extends A
{
B()
{
System.out.println(a);
}
}
class C
{
C()
{
System.out.println(new B().a);//error
}
}
最佳答案
因为包是非相关类的protected
访问修饰符的“可见性限制”。请在此处查看文档:http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html :
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
从上面可以看出,你既不是子类的情况,也不是同包的情况。
关于java - 为什么 protected 子类的不同包中的其他类私有(private),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11344962/