我注意到访问修饰符的奇怪行为
package protected_test.pack1;
public class Source {
protected int protectedInt= 1;
}
package protected_test.pack2;
import protected_test.pack1.Source;
public class Child extends Source{
public static void main(String[] args) {
System.out.println(new Child().protectedInt);//line 1
System.out.println(new Source().protectedInt);//line 2
}
}
第1行编译良好但第2行编写The field Source.protectedInt is not visible
在此之前,我对这种差异一无所知。您能否提供详细的可见性解释?