访问权限


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); // 报错
    }

}

相关文章:

  • 2021-09-26
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-10-17
  • 2021-04-19
猜你喜欢
  • 2021-05-24
  • 2022-12-23
  • 2021-09-09
  • 2021-06-08
  • 2021-10-10
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案