首頁 > Java > java教程 > 主體

Java 中受保護的關鍵字

WBOY
發布: 2024-08-30 15:22:35
原創
264 人瀏覽過

受保護的關鍵字是用來限制變數、方法和建構函式的存取範圍的關鍵字。它是Java中存取修飾符的一種類型。它們用於區分方法、變數、建構函數和類別的範圍。 Java中有4種類型的存取修飾符,它們是:

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗
  1. 預設關鍵字:它們只能在包內訪問,不能在包外調用。顧名思義,當未提及存取說明符時,它會自動指派為預設值。
  2. 公用關鍵字:可以從程式中的任何位置存取它們。這意味著它可以在相同或不同的類別以及相同或不同的套件中使用。
  3. 私人關鍵字:它們將關鍵字限制在更高的級別,不允許從類別本身之外的任何地方存取它們。
  4. 受保護關鍵字:在本文中,我們將了解更多有關受保護關鍵字的資訊。

一旦變數或方法被標記為受保護,就只能透過以下方法存取:

  • 在聲明它的同一個類別中。
  • 來自與聲明的類別也位於同一套件中的其他類別。
  • 從聲明的類別繼承的類,無論其套件如何。

受保護的關鍵字就像是公用和私人關鍵字的組合,因為引入它們是為了存取類別外部的變數(這在私人關鍵字的情況下是不可能的),並維護只有某些方法可以繼承相同的變數。

文法

受保護的關鍵字是用其前綴為「protected」的關鍵字來聲明的。我們首先在名為「MyClass」的類別中宣告 protected 關鍵字,如下所示:

class MyClass {
protected String name = "Katy";
protected int token= 55;
}
public class SubClass extends MyClass {
public static void main(String[] args) {
SubClass obj = new SubClass();
System.out.println(obj.name + "'s token number is: " + obj.token);
}
}
登入後複製

這裡的類別“SubClass”擴展了“MyClass”,因此可以透過建立 SubClass 的物件並呼叫變數來使用 protected 關鍵字。

輸出:

 Java 中受保護的關鍵字

受保護的關鍵字只能在成員層級使用,即在函數外部宣告的非靜態內部類別。 protected 關鍵字與 private 不同,因為它們可以在類別外部和另一個套件的子類別中存取。

使用受保護關鍵字的一些限制是:

  • 它們不能用於將類別宣告為受保護。
  • 介面不能宣告為受保護。
  • 只能透過繼承來存取包外部。
  • 受保護的建構函式無法透過建立其實例在包外部存取。

Java 中受保護關鍵字的範例

讓我們透過一些範例來更好地理解受保護關鍵字的概念。

1.呼叫 protected 關鍵字而不擴充父類別

這裡我們嘗試從「package1」的父類別中呼叫關鍵字。 「ProtectedExample2」是在「package2」中建立的,這裡呼叫了關鍵字「disp」。但是程式碼將無法存取該關鍵字,因為子類別沒有從主類別繼承其值,並且會拋出異常,如圖所示。

代碼:

package com.package1;
public class Example {
protected String disp="Printing message from protected variable from package1";
}
//Create new package as com.package2
//Create new class as ProtectedExample2
package com.package2;
import com.package1.Example;
public class ProtectedExample2 {
public static void main(String[] args) {
ProtectedExample2 a=new ProtectedExample2();
System.out.println(a.disp);
}
}
登入後複製

輸出:

Java 中受保護的關鍵字

2.存取受保護的類別

在此範例中,我們嘗試存取受保護的類別「ProtectedExample5」。這會導致編譯錯誤。

代碼:

protected class ProtectedExample5 {
void display()
{
System.out.println("Try to access outer protected class");
}
public static void main(String[] args) {
ProtectedExample5 p=new ProtectedExample5();
p.display();
}
}
登入後複製

輸出:

Java 中受保護的關鍵字

3.顯示來自同一包但不同類別的受保護關鍵字

在下面的範例中,我們先建立一個名為「com.package1」的套件,並建立一個名為「Example」的新類別。在這裡我們聲明我們的關鍵字“disp”受到保護。我們將嘗試使用「Example1」類別來顯示這個受保護的關鍵字。為此,首先需要建立父類別「Example1」的對象,然後列印指派給關鍵字「disp」的值。

代碼:

package com.package1;
public class Example {
protected String disp="Printing message from protected variable from package1";
}
class Example1 {
public static void main(String[] args) {
Example obj=new Example();
System.out.println(obj.disp);
}
}
登入後複製

輸出:

Java 中受保護的關鍵字

4. Displaying protected keyword from a different package

Using the same code as above, we shall see how to call the protected keyword by creating a different package, “package2”. A protected keyword can be accessed only through inheritance from package1; hence “ProtectedExample2” is extended from “Example”. In a similar way as the first example, we have to create an object of the class “ProtectedExample2” in order to access the protected keyword from package “com.package1”.

Code:

package com.package2;
import com.package1.Example;
public class ProtectedExample2 extends Example{
public static void main(String[] args) {
ProtectedExample2 a=new ProtectedExample2();
System.out.println(a.disp);
}
}
登入後複製

Output:

Java 中受保護的關鍵字

5. Accessing a protected class by overriding to sub-class

Here the class is declared as protected inside the inherited class “Example5”. Also, a protected class called “Example” is declared outside the function but in the same package. When an object of “Example5” is created and the protected class “disp()” is called, we can observe that the overridden method is called instead of the outside class. This is because we shall not be able to import “com.package1” and its class “Example” since it is not visible and causes a compilation error.

Code:

//Create a file by Example.java
package com.package1;
class Example
{
protected void disp()
{
System.out.println("Printing from protected class in the outside function");
}
}
//Create a class by the name Example5.java
public class Example5 extends Example {
protected void disp()
{
System.out.println("Accessing the overriden function");
}
public static void main(String[] args) {
Example5 exp=new Example5();
exp.disp();
}
}
登入後複製

Output:

Java 中受保護的關鍵字

Importance of Protected Keyword

The importance of protected keyword in java is:

  • These keywords allow the classes or their variables to be inherited from their parent class which is not possible with any other restricted keyword such as private.
  • A protected keyword is the combination of a private keyword’s advantage and that of a public keyword. It eliminates the disadvantage of public keyword that the variable or class be accessible from anywhere in the program by restricting the scope.

Conclusion

As shown in the above examples, we choose protected keywords depending on the access level we require at the code level. They help greatly in cases where the same variable or class needs to be accessed from other inherited methods in the program. A parent-child relationship is always present between the parent class and its sub-classes which are using the protected keyword.

以上是Java 中受保護的關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!