访问修饰符的代码演示:
package com.etc.day03.lianxi;
public class Demo1 {
private String name;
String sex;
protected String id;
public String num;
}
package com.etc.day03.lianxi;
public class Demo2 {
public static void main(String[] args) {
Demo1 demo1 = new Demo1();
//default
demo1.sex = "111";
//protected
demo1.id = "222";
//public
demo1.num = "333";
}
}
package com.etc.day03.lianxi1;
import com.etc.day03.lianxi.Demo1;
public class Demo3 extends Demo1 {
//不同包的子类,在方法里面可以调用protected
public void t(){
id="12";
}
}
package com.etc.day03.lianxi1;
import com.etc.day03.lianxi.Demo1;
public class Demo4 {
public void a(){
//public的访问范围(只要new出来就可以访问到)
Demo1 demo1 = new Demo1();
demo1.num = "111";
}
}