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
}
}