Java 中的 private、protected 和 final 訪問修飾符
每當我們宣告一個類時,都需要提供它的訪問級別,以便 JVM 可以知道程式中是否可以發生繼承或例項化,以及類的作用域是什麼。為此,我們使用訪問修飾符,這些修飾符可以用於類、方法和變數等。
private 訪問修飾符將方法或變數的訪問限制在類內;這意味著只能在同一個類中訪問該方法或變數。
protected 訪問修飾符允許訪問該類及其任何子類,包括其他包中的類。
final 訪問修飾符不允許擴充套件類。如果應用於方法,則子類不能覆蓋它。如果宣告一個變數為 final,則在初始化後不能對其重新賦值。
演算法
要實現 private、protected 和 final 訪問修飾符,請按照以下步驟操作:
步驟 1 − 定義一個類及其成員,即方法和變數。
步驟 2 − 要將變數或方法宣告為 private,請使用 private 關鍵字作為訪問修飾符。
步驟 3 − 要將變數或方法宣告為 protected,請使用 protected 關鍵字作為訪問修飾符。
步驟 4 − 使用 final 關鍵字定義變數為 final。
步驟 5 − 現在,同一個類的成員可以訪問 private 變數或方法。
步驟 6 − 同一個類或子類的方法可以訪問 protected 變數或方法。
步驟 7 − 要在類外部訪問 private 和 protected 變數,可以使用訪問這些變數的 public 方法。
步驟 8 − 訪問 private 和 protected 變數的方法應宣告為 public。
步驟 9 − 要建立可在類內訪問的常量,請同時使用 private 和 final 關鍵字。
步驟 10 − 要建立可在類和子類中訪問的常量,請同時使用 protected 和 final 關鍵字。
語法
public class Way2Class { private int privateVariable; protected int protectedVariable; final int finalVariable = 5; private void privateMethod() { // method implementation } protected void protectedMethod() { // method implementation } }
方法 1:使用 private 訪問修飾符
程式碼片段如下
示例
public class Main { private String name; private int age; public Main(String name, int age) { this.name = name; this.age = age; } private String getDetails() { return "Name: " + name + ", Age: " + age; } public void displayDetails() { String details = getDetails(); System.out.println(details); } public static void main(String[] args) { Main john = new Main("John", 30); john.displayDetails(); // prints "Name: John, Age: 30" String details = john.getDetails(); // Accessing private method, results in a compile-time error String name = john.name; // Accessing private field, results in a compile-time error int age = john.age; // Accessing private field, results in a compile-time error } }
輸出
Name: John, Age: 30
解釋
在上面的程式中,如果我們在 Employee 類外部呼叫 getDetails() 方法和 private 變數,則會得到編譯時錯誤。
方法 2:使用 protected 訪問修飾符
程式碼示例如下
示例
public class Main { public static void main(String[] args) { Car honda = new Car("Honda", "Civic", 2022, 4); honda.displayDetails(); } } class Vehicle { protected String make; protected String model; protected int year; public Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } protected String getDetails() { return "Make: " + make + ", Model: " + model + ", Year: " + year; } } class Car extends Vehicle { private int numDoors; public Car(String make, String model, int year, int numDoors) { super(make, model, year); this.numDoors = numDoors; } public void displayDetails() { String details = getDetails() + ", Number of Doors: " + numDoors; System.out.println(details); } }
輸出
Make: Honda, Model: Civic, Year: 2022, Number of Doors: 4
解釋
以下程式碼執行沒有錯誤,因為我們在同一個包、類和子類中訪問 protected 變數和方法。
方法 3:使用 final 訪問修飾符
示例
class Animal { final String species; public Animal(String species) { this.species = species; } public void makeSound() { System.out.println("The " + species + " makes a sound."); } } class Cat extends Animal { public Cat() { super("Cat"); } @Override public void makeSound() { System.out.println("Meow!"); } } public class Main { public static void main(String[] args) { Cat cat = new Cat(); cat.makeSound(); } }
輸出
Meow!
解釋
Cat 類擴充套件 Animal 類,並且不新增任何新的例項變數,但是它試圖用自己的實現覆蓋 makeSound() 方法,該實現只是打印出“Mawo!”。但是,由於 Animal 類中的 makeSound() 方法被標記為 final,因此此嘗試會導致編譯時錯誤。
不同方法的比較
Private 修飾符 |
Protected 修飾符 |
Final 修飾符 |
---|---|---|
成員可以在宣告它們的同一個類中訪問。 |
成員可以在同一個包中的類和子類中訪問。 |
成員不能在同一個類或子類中訪問。 |
介面可以使用此修飾符 |
介面可以使用此修飾符 |
介面不能使用此修飾符 |
Private 方法可以被覆蓋 |
Protected 方法不能被覆蓋 |
Final 方法不能被覆蓋。 |
結論
Java 中的這些訪問修飾符用於控制類成員(變數、方法和內部類)的可見性。Private 將訪問限制在同一個類中,protected 允許訪問子類和同一個包中的類,final 表示成員的值不能更改或方法不能被覆蓋。