首頁 > Java > java教程 > 主體

Java @Override

WBOY
發布: 2024-08-30 16:22:43
原創
766 人瀏覽過

@Override 註解用於當開發人員重寫 Java 中的函數以使用相同的函數名稱但為這些函數指派不同的屬性時。如果您知道 Java 中的過度函數,但尚未使用 @override 註釋,因為您不想將其用作明確編寫它的強制選項。自 Java 1.5 引入以來,它預設為啟動狀態。它促進運行時多態性。這是因為我們可以在不使用註解的情況下重寫任何函數。儘管如此,它仍然有一個主要優點:如果編譯器偶然錯過了重寫(就像開發人員在重寫函數名稱中犯了拼字錯誤)。在重寫註解的幫助下,編譯器將理解並用子函數重寫基底函數。它還提高了程式碼的可讀性,減少了維護時間和精力。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法:

public @interface Override
登入後複製

符號「@」應該出現在覆蓋關鍵字之前,以便編譯器了解這是否是註釋。重寫函數在基底類別和繼承類別中應具有相同的定義以及傳回類型和多個參數。如果其中任何一個存在差異,則不會將其視為覆蓋函數,同時將此函數理解為新函數。

@Override 註解在 Java 中如何運作?

  • 重寫註解在繼承類別中定義的重寫函數之前使用,以確保編譯器理解該函數是故意在兩個不同的類別中使用相同的參數和返回類型定義的。
  • 為了讓系統了解要呼叫哪個函數,因為該函數在基底類別和繼承類別中具有相同的參數,我們必須使用實例來呼叫該函數。
  • 如果使用父類別的物件呼叫函數,則呼叫父類別函數及其本地函數定義,而如果使用繼承類別的對象,則呼叫繼承類別的函數。

範例:

Base class {}
Child class{}
Base object1= new Base();// Here Base class is instantiated so the function definition will be called from base class.
Base object2= new Child(); /// Here Child class is instantiated so the
function definition will be called from child class
登入後複製
  • 現在上面定義的名為「object1」和「object2」的物件與點運算子一起使用來提取函數定義。
  • 所有這些函數只有在程式碼實際執行開始的主類別下維護時才有效。
  • 控制項將命中主類,然後搜尋主類上方預先定義的類別的物件實例。
  • 然後該物件將呼叫被呼叫的函數。
  • 如果已經在類別定義中定義為原型,也可以在函數中傳遞所需的參數。
  • 一個基底類別可以被多個類別繼承;唯一的區別是物件建立和使用該物件呼叫函數。

Java @Override 範例

以下是範例:

範例#1

示範覆蓋註解工作原理的範例。

說明:

下面的程式中定義了兩個類:一個是基類,也稱為父類“Pclass”,另一個類“Cclass”,繼承了基類的屬性和成員函數,是稱為繼承類或子類別。該函數首先在父類別中聲明。在此範例中,函數名稱為 printfunction(),它負責列印作為參數傳遞的字串。

在名為「Cclass」的繼承類別中宣告和定義同名函數,並在其前面帶有 @override 註解。其他字串作為參數傳遞給它。在主類別中,上面定義的類別透過創建它們的物件來實例化。 「object1」標識Pclass 的對象,「object2」標識Cclass 的對象。使用這些不同的物件呼叫相同的函數。在第一種情況下,object1 從父類別 Pclass 取得字串。而稍後,當呼叫 object2 時,@override 註解就會起作用並更改內容字串。這是 Java 下提供的一個重要工具,用於實現易於理解的程式碼和更好的功能。

代碼:

// This is Base class
class Pclass {
void printfunction()
{
System.out.println("This is the output of function present in parent class \"Pclass\". ");
}
}
// This is Child class
class Cclass extends Pclass {
// The below function is override function along with override annotation
@Override
void printfunction()
{
System.out.println("This is the output of function present in child class \"Cclass\".");
}
}
// Thi is Main class from here the contro; execution starts. JAVA compiler searches for main class to start executing any code.
public class Main {
public static void main(String[] args)
{
Pclass object1 = new Pclass();
object1.printfunction();
Pclass object2 = new Cclass();
object2.printfunction();
}
}
登入後複製

輸出:

這是一個帶有兩行字串的輸出螢幕。第一個字串行來自基底函數,而第二個字串行來自繼承類別中定義的重寫函數。

Java @Override

Example #2

Here we have one base class with two child classes inheriting it. The second inherited class is instantiated, and the output string is triggered from the 2nd inherited class.

Code:

class Pclass {
void printfunction()
{
System.out.println("This is the output of function present in parent class \"Pclass\". ");
}
}
// This is Child class
class Cclass extends Pclass {
// The below function is override function along with override annotation
@Override
void printfunction()
{
System.out.println("This is the output of function present in child class \"Cclass\".");
}
}
// This is Child class
class Cclass2 extends Pclass {
// The below function is override function along with override annotation
@Override
void printfunction()
{
System.out.println("This is the output of function present in child class number 2 \"Cclass\".");
}
}
// This is Main class from here the contro; execution starts. JAVA compiler searches for main class to start executing any code.
public class Main {
public static void main(String[] args)
{
Pclass object1 = new Pclass();
object1.printfunction();
Pclass object2 = new Cclass2();
object2.printfunction();
}
}
登入後複製

Output:

Java @Override

Conclusion

Hence Java override function comes with a lot of benefits like providing run-time polymorphism, easy code access, clean code and many more. Adding override annotation assures that the compiler understands the intention of function definition via function declarations in classes. This is one of the important properties of the oops concept called polymorphism.

以上是Java @Override的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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