# 访问权限

java类的访问权限分为两种:公共类和非公共类,public修饰为公共类,没有使用public修饰的类为非公共类。
使用场景:

  • 如果这个类想要在其他包中使用就定义为公共类
  • 非公共类只能在当前包中使用
访问权限 当前类 当前包 派生类(不在当前包) 其它包
private(私有的) 可以
默认(没有权限修饰符) 可以 可以
protected(受保护的) 可以 可以 可以
public(公有的) 可以 可以 可以 可以

备注:

  • 派生类 A类继承B类,B称为基类、父类、超类,A称为子类、派生类、扩展类
  • private 只能在当前类访问
  • 默认 当前类、当前包访问
  • protected 当前类、当前包、派生类(不同包)访问
  • public 能在当前类、当前包、派生类、其它包访问
// Father.java
package com.qym.xmm.day19;

public class Father {
    public String name;
    private String idCard;
    protected int age;
    String sex;

    public void pt() {
        System.out.println(this.name); // null
        System.out.println(this.idCard); // null
        System.out.println(this.age); // null
        System.out.println(this.sex); // null
    }
}

// Son.java
package com.qym.xmm.day19;

public class Son extends Father {
    public void pt() {
        // public
        System.out.println(this.name); // null
        // private
//        System.out.println(this.idCard); // null // java: idCard 在 com.qym.xmm.day19.Father 中是 private 访问控制
        // protected 受保护的
        System.out.println(this.age); // null
        // 默认
        System.out.println(this.sex); // null
    }
}

// Test01.java
package com.qym.xmm.day19;

public class Test01 {
    public static void main(String[] args) {
        Father father = new Father();
        // public
        System.out.println(father.name); // null
        // private
//        System.out.println(father.idCard); // java: idCard 在 com.qym.xmm.day19.Father 中是 private 访问控制
        // protected 受保护的
        System.out.println(father.age); // null
        // 默认
        System.out.println(father.sex); // null
        System.out.println("-----------------");
        father.pt();
        Son son = new Son();
        son.pt();
    }
}
// 测试在不同包中的派生类访问
//Son.java
package com.qym.xmm.day19_;

import com.qym.xmm.day19.Father;

public class Son extends Father {
    public void pt(){
        // public
        System.out.println(this.name); // null
        // private
//        System.out.println(this.idCard); // null // java: idCard 在 com.qym.xmm.day19.Father 中是 private 访问控制
        // protected 受保护的 派生类不在当前包中可以访问
        System.out.println(this.age); // null
        // 默认  派生类,不在当前包中不可以访问
//        System.out.println(this.sex); // null //java: sex在com.qym.xmm.day19.Father中不是公共的; 无法从外部程序包中对其进行访问
    }
}

//Test01.java
package com.qym.xmm.day19_;

import com.qym.xmm.day19.Father;
import com.qym.xmm.day19_.Son;

public class Test01 {
    public static void main(String[] args) {
        Father father = new Father();
        // public
        System.out.println(father.name); // null
        // private 私有的 其它包引入报错
//        System.out.println(father.idCard); // java: idCard 在 com.qym.xmm.day19.Father 中是 private 访问控制
        // protected 受保护的  其它包引入报错
//        System.out.println(father.age); // null //java: age 在 com.qym.xmm.day19.Father 中是 protected 访问控制
        // 默认 其它包引入报错
//        System.out.println(father.sex); // null //java: sex在com.qym.xmm.day19.Father中不是公共的; 无法从外部程序包中对其进行访问
        System.out.println("-----------------");
        father.pt();

        Son son = new Son();
        son.pt();
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

Java方法覆盖中的访问权限,访问覆盖(重写)的规则:

  • 子类方法签名必须和父类方法一致, 方法签名就是方法名与参数列表
  • 子类方法的返回值类型可以和父类方法返回值类型一样, 也可以是父类方法返回值类型的子类型
  • 子类方法的访问权限可以和父类方法访问权限相同,也可以比父类方法访问权限更宽泛(更大),如果父类方法是public修饰,子类方法只能是public修饰,如果父类方法是protected修饰,子类方法可以protected/public修饰
  • 子类方法的异常不能比父类方法的异常更大