用于定义访问权限修饰符的关键字
Java 中,可以使用访问控制符来保护对类、变量、方法和的构造方法的访问。
修饰符 | 当前类 | 同一包内 | 子孙类(同一包) | 子孙类(不同包) | 其他包 |
---|---|---|---|---|---|
public | Y | Y | Y | Y | Y |
protected | Y | Y | Y | Y/N | N |
default | Y | Y | Y | N | N |
private | Y | N | N | N | N |
public
对所有类可见。使用对象:类、接口、变量、方法。
注意:
- 如果几个相互访问的 public 类分布在不同的包中,则需要导入相应 public 类所在的包。由于类的继承性,类所有的公有方法和变量都能被其子类继承。
- 接口里的变量都隐式声明为 public static final,而接口里的方法默认情况下访问权限为 public。
- Java 程序的 main() 方法必须设置为公有的,否则,Java 解释器将不能运行该类。
public class Test {
interface Animal {
[public static final ]String foot = "YES";
public void eat();
void travel();
}
public static void main(String[] args) {
public int num = 0;
// ...
}
}
protected
对同一包内的类和所有子类可见。使用对象:变量、方法,不能修饰类(内部类除外)。
注意:
- 子类与基类在同一包中:被声明为 protected 的变量、方法和构造器可以被同一包中的任何其他类访问;
- 子类与基类不在同一包中:在子类中,子类实例可以访问其从基类继承而来的 protected 方法,而不能访问基类实例的 protected 方法。
- 接口及接口的成员变量和成员方法不能声明为 protected。
在遇到涉及 protected 成员的调用时:首先要确定出该 protected 成员来自何方,其可见性范围是什么,然后就可以判断出当前用法是否可行了。如下例子:
package test1;
public class Father {
protected void fatherMethod() {
// ...
}
}
package test1;
public class Son1 extends Father {}
package test2;
public class Son2 extends Father {}
package test1;
public class Test {
public static void main(String[] args) {
Son1 son1 = new Son1();
son1.fatherMethod(); // Compile OK --- (1)
son1.clone(); // Compile Error --- (2)
Son2 son2 = new Son2();
son2.fatherMethod(); // Compile OK --- (3)
son2.clone(); // Compile Error --- (4)
}
}
对于上面的例子:
- 对于 (1) (3),fatherMethod() 方法是从类 Father 继承而来的,其可见性是包 test1 及其子类 Son1 和 Son2,而且调用 fatherMethod() 方法的类 Test 所在包也是 test1,因此可以通过编译。
- 对于 (2) (4),clone() 方法的可见性是 java.lang 包及其所有子类,对于语句”son1.clone();“和”son2.clone();“,二者的 clone() 在类 Son1 和 Son2 中是可见的,但对 Test 是不可见的,所以编译不通过。
package test1;
class MyObject {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package test2;
public class Test extends MyObject {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile Error --- (1)
Test test = new Test();
test.clone(); // Compile OK --- (2)
}
}
对于上面的例子:
- 对于 (1) 而言,clone() 方法来自于类 MyObject 本身,因此其可见性为包 test1 及 MyObject 的子类,虽然 Test 是 MyObject 的子类,但是 Test 中不能访问基类 MyObject 实例 obj 的 protected 方法 clone(),因此编译不通过;
- 对于 (2) 而言,由于 Test 中访问的是其本身实例从基类 MyObject 继承来的 clone(),因此编译通过。
package test1;
class MyObject extends Test {}
package test2;
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile OK --- (1)
}
}
对于上面的例子:clone() 方法来自于类 Test,因此其可见性为包 test2 及其子类 MyObject,而 (1) 正是在 test2 的类 Test 中调用,属于同一包,编译通过。
package test1;
class MyObject extends Test {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package test2;
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile Error --- (1)
}
}
对于上面的例子:clone() 方法来自于类 MyObject,因此其可见性为包 test1 及其子类(此处没有子类),而类 Test 却在包 test2 中,不满足可见性,因此编译不通过。
package test;
class MyObject {
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone(); // Compile OK --- (1)
}
}
对于上面的例子:clone() 方法来自于类 MyObject,因此其可见性为包 test 及其子类(此处没有子类),而类 Test 也在包 test 中,因此满足可见性,编译通过。
package test;
class MyObject extends Test {}
public class Test {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.clone();
}
}
对于上面的例子:clone() 方法来自于类 Test, 因此其可见性为包 test 及其子类 MyObject,而类 Test 也在包 test 中,因此满足可见性,编译通过。
package test;
class MyObject extends Test {
public static void main(String[] args) {
Test test = new Test();
test.clone(); // Compile Error --- (1)
}
}
public class Test {}
对于上面的例子:clone() 方法来自于类 Object,因此该 clone() 方法可见性为包 java.lang 及其子类 Test,由于类 MyObject 不在此范围内,因此不满足可见性,编译不通过。
default
即默认,什么都不写。在同一包内可见,不使用任何修饰符。适用对象:类、接口、变量、方法。
String str = "Doris";
boolean judge() {
return true;
}
private
在同一类内可见。使用对象:变量、方法,不能修饰类和接口(内部类除外)。
注意:
- 声明为私有访问类型(private)的变量只能通过类中公共的 getter 方法被外部类访问。
- private 访问修饰符的使用主要用来隐藏类的实现细节和保护类的数据。
public class Draw {
private int length;
public int getLength() {
return this.length;
}
public void setLength(int length) {
this.length = length;
}
}
上面的例子中,Draw 类中的 length 变量为私有变量,所以其他类不能直接得到和设置该变量的值。为了使其他类能够操作该变量,定义了两个 public 方法:getLength() (返回 length 的值)和 setLength() (设置 length 的值)。
访问控制和继承
注意以下方法继承的规则:
- 父类中声明为 public 的方法,在子类中也必须声明为 public。
- 父类声明为 protected 的方法,在子类中要声明为 protected 或者 public,但不能是 private。
- 父类中声明为 private 的方法,不能够被继承。