Java 修饰符
修饰符
到目前为止,你已经非常熟悉 public
关键字了,它几乎出现在我们所有的示例中
public class Main
public
关键字是一个访问修饰符,这意味着它用于设置类、属性、方法和构造函数的访问级别。
我们将修饰符分为两类
- 访问修饰符 - 控制访问级别
- 非访问修饰符 - 不控制访问级别,但提供其他功能
访问修饰符
对于类,你可以使用 public
或 default
修饰符 | 描述 | 试一试 |
---|---|---|
公共 |
该类可被任何其他类访问 | 试一试 » |
default | 该类只能被同一包中的类访问。当你没有指定修饰符时,就会使用此项。你将在Packages 章节中了解更多关于包的知识。 | 试一试 » |
对于属性、方法和构造函数,你可以使用以下之一
修饰符 | 描述 | 试一试 |
---|---|---|
公共 |
代码可被所有类访问 | 试一试 » |
private |
代码只能在声明的类中访问 | 试一试 » |
default | 代码只能在同一包中访问。当你没有指定修饰符时,就会使用此项。你将在Packages 章节中了解更多关于包的知识。 | 试一试 » |
protected |
代码可在同一包中访问,并且子类也可以访问。你将在Inheritance 章节中了解更多关于子类和超类的知识。 | 试一试 » |
非访问修饰符
对于类,你可以使用 final
或 abstract
修饰符 | 描述 | 试一试 |
---|---|---|
final |
该类不能被其他类继承(你将在Inheritance 章节中了解更多关于继承的知识)。 | 试一试 » |
abstract |
该类不能用于创建对象(要访问抽象类,必须先继承它。你将在Inheritance 和 Abstraction 章节中了解更多关于继承和抽象的知识)。 | 试一试 » |
对于属性和方法,你可以使用以下之一
修饰符 | 描述 |
---|---|
final |
属性和方法不能被重写/修改 |
static |
属性和方法属于类,而不是对象 |
abstract |
只能在抽象类中使用,并且只能用于方法。该方法没有方法体,例如 abstract void run();。方法体由子类提供(继承而来)。你将在Inheritance 和 Abstraction 章节中了解更多关于继承和抽象的知识。 |
transient |
在序列化包含它们的类的对象时,会跳过属性和方法 |
synchronized |
同一时间只能由一个线程访问方法 |
volatile |
属性的值不会被线程本地缓存,并且总是从“主内存”读取 |
Final
如果你不想覆盖现有属性的值,请将属性声明为 final
。
示例
public class Main {
final int x = 10;
final double PI = 3.14;
public static void main(String[] args) {
Main myObj = new Main();
myObj.x = 50; // will generate an error: cannot assign a value to a final variable
myObj.PI = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}
Static
一个 static
方法意味着可以在不创建类的对象的情况下访问它,这与 public
不同。
示例
一个示例,演示 static
和 public
方法之间的区别
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[ ] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would output an error
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method
}
}
Abstract
一个 abstract
方法属于一个 abstract
类,并且没有方法体。方法体由子类提供。
示例
// Code from filename: Main.java
// abstract class
abstract class Main {
public String fname = "John";
public int age = 24;
public abstract void study(); // abstract method
}
// Subclass (inherit from Main)
class Student extends Main {
public int graduationYear = 2018;
public void study() { // the body of the abstract method is provided here
System.out.println("Studying all day long");
}
}
// End code from filename: Main.java
// Code from filename: Second.java
class Second {
public static void main(String[] args) {
// create an object of the Student class (which inherits attributes and methods from Main)
Student myObj = new Student();
System.out.println("Name: " + myObj.fname);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduationYear);
myObj.study(); // call abstract method
}
}