JLS 6.6.2为我们提供了对 protected 成员的包访问的以下限制。
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
负责实现是什么意思?你就不能举个例子吗?
最佳答案
这意味着您无法访问同一类的不同实例的 protected 父类(super class)成员。
package one;
public class A {protected int b;}
package two;
public class B extends A {
public void someMethod (A other)
{
b = 5; // allowed
other.b = 5; // not allowed
}
}
关于java - protected 访问修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31050527/