protected,可以让子类在不同的包中访问父类的数据或方法,但是要import,直接写不行,比如
父类
package chapter09.Test_11.package1;
public class C1 { public int x; protected int y; int z; private int u; protected void m(){ System.out.println("hello"); }}子类
package chapter09.Test_11.package2;
//import java07.TestPackage.p1.C1;//这是易犯错误,ctrl+1直接选一个导入,但同名的类太多,导入错了
import chapter09.Test_11.package1.C1;
public class C4 extends C1{ }
另外。
C5不继承C1,在C5类中声明的C1的对象o,只能访问o.x,其他的都不行
package chapter09.Test_11.package2;
import chapter09.Test_11.package1.C1;
public class C5{ C1 o=new C1();//这是C1 private int printX=o.x; private int printY=o.y; private int printZ=o.z; private int printU=o.u; o.m();}