访问权限
1. public 公共的
2. private 私有的
3. default 包内的
package com.kjy.entity;
public class Person {
public String pub = "public"; // 公共的
private String pri = "private"; // 私有的
String def = "default"; // 包内的
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.pub);
System.out.println(p.pri);
System.out.println(p.def);
}
}
// 在同个包内,private报错
package com.kjy.entity;
public class PersonText {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.pub);
System.out.println(p.pri); // 报错
System.out.println(p.def);
}
}
//在不同的包内,private,default报错
package com.kjy.dao;
import com.kjy.entity.Person;
public class PersonText1 {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.pub);
System.out.println(p.pri); // 报错
System.out.println(p.def); // 报错
}
}