Java 访问控制符

访问控制符指定了类之间的访问范围。

分类

  • public
    表示对外公开,所有类都可以访问。
  • default
    没有修饰符,表示同一个包内公开,也可以理解为包内私有。即同一个包内的类可以访问。
  • protected
    不管是包内还是包外的子类,以及同一个包内的类公开。即同一个包内和所有子类能访问。
  • private
    只有当前类内部可以访问。

访问范围

Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

0047-java-accsee-control.png

示例

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
99
// 1. BaseBean
package com.***.inner;
public class BaseBean {

private class InnerClass{
private String innerPrivateStr;
String innerDefaultStr;
protected String innerProtectedStr;
public String innerPublicStr;

public InnerClass(){
innerPrivateStr = "innerPrivateStr";
innerDefaultStr = "innerDefaultStr";
innerProtectedStr = "innerProtectedStr";
innerPublicStr = "innerPublicStr";
}
}

private String privateStr;
String defaultStr;
protected String protectedStr;
public String publicStr;

public BaseBean(){
privateStr = "privateStr";
defaultStr = "defaultStr";
protectedStr = "protectedStr";
publicStr = "publicStr";
}

public void show(){
InnerClass innerClass = new InnerClass();
System.out.println("BaseBean:InnerClass: " + innerClass.innerPrivateStr);
System.out.println("BaseBean:InnerClass: " + innerClass.innerDefaultStr);
System.out.println("BaseBean:InnerClass: " + innerClass.innerProtectedStr);
System.out.println("BaseBean:InnerClass: " + innerClass.innerPublicStr);

System.out.println("BaseBean: " + privateStr);
System.out.println("BaseBean: " + defaultStr);
System.out.println("BaseBean: " + protectedStr);
System.out.println("BaseBean: " + publicStr);
}
}

// 2. SubBean in the same package.
package com.***.inner;
public class SubBean extends BaseBean {

public void show(){
System.out.println("SubBean: " + defaultStr);
System.out.println("SubBean: " + protectedStr);
System.out.println("SubBean: " + publicStr);
}

}

// 3. SubBean in the other package.
package com.***.outer;
public class OuterPackageSubBean extends BaseBean{

public void show(){
System.out.println("OuterPackageSubBean: " + protectedStr);
System.out.println("OuterPackageSubBean: " + publicStr);
}

}

// 4. Test.
package com.***.test;
public class TestAccessControl {
BaseBean baseBean;
SubBean subBean;
OuterPackageSubBean outerPackageSubBean;

public TestAccessControl(){
baseBean = new BaseBean();
subBean = new SubBean();
outerPackageSubBean = new OuterPackageSubBean();
}

public void show(){
System.out.println("********The Same Class.########");
baseBean.show();
System.out.println("********The Sub Class in the same package.########");
subBean.show();
System.out.println("********The Sub Class in other packages.########");
outerPackageSubBean.show();
System.out.println("********The World.########");
System.out.println("TestAccessControl: baseBean " + baseBean.publicStr);
System.out.println("TestAccessControl: subBean " + subBean.publicStr);
System.out.println("TestAccessControl: outerPackageSubBean "
+ outerPackageSubBean.publicStr);
}

public static void main(String[] args) {
TestAccessControl testAccessControl = new TestAccessControl();
testAccessControl.show();
}
}

特点

  • 方法内部的局部变量是不存在访问控制符的概念的,不能使用访问控制符修饰
  • 在外部类整个类内部这个范围内,是不受访问控制符限制的,即 private 变量外部类和内部类是可以相互直接访问到的。所以内部类中通常会省略访问控制符。
  • 接口会默认将所有成员设置为 public

参考文档

0%