Super 是一個關鍵字,用來呼叫超類別中的函數或方法。這將在子類別中定義。只能使用此關鍵字呼叫公共和受保護的方法。也就是說,私有方法和靜態方法不能用this來呼叫。 java 中的 super 關鍵字也可以用來呼叫父類別的建構子。 super 關鍵字的語法、範例和更多詳細資訊將在以下部分中討論。
文法
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
super.> or super([0 or more arguments]);
超級關鍵字在 Java 中如何運作?
正如已經提到的,super 可以在多種場合使用。
- 引用直接父類別的實例變數。
- 引用直接父類別的方法。
- 引用直接父類別的建構子。
1.引用直接父類別的實例變數
如果父類別和子類別有相同的資料成員,可以使用Super關鍵字來存取父類別的欄位或資料成員。在這種情況下,Java 虛擬機器可能會出現歧義。
範例:
代碼:
class A { protected String name="ann"; } class B extends A { public String name="Anna"; public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } }
這裡,兩個類別 A 和 B 有一個共同的欄位名稱。子類別中的函數 printType() 使用 super 關鍵字來引用父類別中的欄位。
2.引用直接父類別的方法
方法重寫是子類別宣告父類別中已有的相同函數或方法的過程。假設,如果子類別的物件呼叫該方法,則只會呼叫子類別中的方法。為了存取父方法,可以使用 super 關鍵字。
範例:
代碼:
class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } class B extends A { public String name="Anna”; public void hello() { System.out.println("I am " + name); } public void test() { hello(); super.hello(); } }
這裡,兩個類別 A 和 B 有相同的方法 hello()。借助 test() 函數中的 super 關鍵字,可以存取父類別的 hello() 方法。
3.引用直接父類別的建構子
眾所周知,在建立類別的物件時會自動呼叫建構函式(預設)。 super 關鍵字可用於從子類別的建構子明確呼叫超類別的建構子。確保 super 僅在子類別的建構函式中使用,並且它是其中的第一個語句。
範例:
代碼:
class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } }
Java 中的超級關鍵字範例
以下是提到的不同範例:
範例#1
在下面的程式中,存在一個公共變數名,並使用 super 來呼叫父類別中的變數。
代碼:
//Java program to illustrate Super keyword to refer instance variable //parent class class A { protected String name="ann"; } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method public void hello() { System.out.println("I am " + name); System.out.println("I am " + super.name); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.hello();//call the method in child class } }
輸出:
範例#2
程式有助於示範 super 關鍵字,同時引用父類別中的相同方法。這裡,hello() 是兩個類別中都可用的方法。
代碼:
//Java program to illustrate Super keyword to refer same method in parent class //parent class class A { protected String name="ann"; public void hello() { System.out.println("I am " + name); } } //child classs class B extends A { public String name="Anna";//variable which is same in parent class //sample method which is same in parent class public void hello() { System.out.println("I am " + name); } //method to call the hello() method in parent and child class public void test() { hello(); super.hello(); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class objb.test();//call the method in child class } }
輸出:
範例 #3
程式使用 super 關鍵字呼叫父類別的建構子。
代碼:
//Java program to illustrate Super keyword to refer constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } } //child class class B extends A { //constructor of child class B() { super(); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
輸出:
範例#4
此程式示範了 super 關鍵字的用法來引用父類別的參數化建構子。
代碼:
//Java program to illustrate Super keyword to refer parameterised constructor in parent class //parent class class A { //constructor of parent class A() { System.out.println("I am Kavya Madhavan"); } //parameterised constructor A(String name) { System.out.println("I am " + name); } } //child class class B extends A { //constructor of child class B() { super("Renuka"); System.out.println("I am Dileep Menon"); } } //main class public class SuperExample { public static void main(String[] args) { B objb=new B();//object of child class } }
輸出:
結論
Super是Java中的關鍵字,用來引用父類別中的方法或函式、實例變數或屬性、建構子。如果未宣告建構函數,編譯器會自動建立預設建構函數。同樣,如果沒有聲明,編譯器會自動呼叫 super()。在這篇文件中,對 super 關鍵字的幾個方面進行了詳細的解釋。
以上是Java 中的 Super 關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。