首頁 > Java > java教程 > 主體

static關鍵字:重載、重寫以及this和super的作用

Barbara Streisand
發布: 2024-10-26 07:33:30
原創
615 人瀏覽過

Static Keyword: Overloading, Overriding, and the Role of this and super

這篇文章擴展了我們先前對static 關鍵字的討論,重點在於靜態上下文中的方法重載方法覆蓋的概念方法。我們也將探討 this 和 super 關鍵字在靜態上下文中如何表現(或不表現)。

如果您不熟悉 static 關鍵字,先了解靜態變數和靜態方法可能會有所幫助。

由於本文涉及 this 和 super 在靜態上下文中的行為,因此您可能還想了解一下 This 關鍵字和 Super 關鍵字。


本文的關鍵主題:

  1. 靜態方法的重載

  2. 為什麼靜態方法不能被覆蓋

  3. 在靜態上下文中使用 this 和 super 關鍵字

  4. 示範關鍵概念的範例


1. 重載靜態方法

在Java中,重載允許同名的方法存在不同的參數清單。靜態方法可以像實例方法一樣被重載。但是,請記住,重載發生在編譯時

程式碼範例:

package keywords.static_keyword;

public class StaticVariables {

    static int idStatic = 1;

    public StaticVariables(String name) {
        this.id = ++idStatic;
        this.name = name;
    }

    int id;
    String name;

    static void displayText() {
        System.out.println("DisplayText called. ID: " + idStatic);
    }

    // Overloaded static method with a parameter
    static void displayText(String name) {
        System.out.println("Overloaded DisplayText called. Name: " + name);
    }

    public static void main(String[] args) {
        StaticVariables.displayText();
        StaticVariables.displayText("Static Overload Example");
    }
}
登入後複製
登入後複製

解釋:

  • 方法重載:displayText方法重載有兩個版本-一個不含參數,一個有字串參數。

  • 這是合法,因為Java可以在編譯時期間根據參數列表區分這兩個方法。


2. 為什麼靜態方法不能被重寫

Java 不允許重寫靜態方法。由於靜態方法綁定到類別而不是物件實例,因此它們不參與運行時多態性,這是方法重寫的基礎。

但是,靜態變數是繼承的並且可以被子類別存取或修改。

程式碼範例:

package keywords.static_keyword;

public class StaticVariables {

    static int idStatic = 1;

    public StaticVariables(String name) {
        this.id = ++idStatic;
        this.name = name;
    }

    int id;
    String name;

    static void displayText() {
        System.out.println("DisplayText called. ID: " + idStatic);
    }

    // Overloaded static method with a parameter
    static void displayText(String name) {
        System.out.println("Overloaded DisplayText called. Name: " + name);
    }

    public static void main(String[] args) {
        StaticVariables.displayText();
        StaticVariables.displayText("Static Overload Example");
    }
}
登入後複製
登入後複製

解釋:

  • 無重寫:註解掉的@Override方法示範了重寫靜態方法的嘗試,這會導致編譯時錯誤,因為重寫僅適用於實例方法。
  • 靜態變數繼承:即使靜態方法不能被重寫,靜態變數也是可以繼承的,並且可以在子類別中存取或修改。
  • 呼叫靜態方法:靜態方法必須使用類別名稱調用,如果它們在同一個類別中,則必須直接調用。

3. 在靜態上下文中使用 this 和 super 關鍵字

  • this 關鍵字:指類別的目前實例。由於靜態方法不對任何實例進行操作,在靜態上下文中使用它會導致編譯時錯誤

例子:

package keywords.static_keyword;

public class CannotOverrideStaticMethod extends StaticVariables {

    public CannotOverrideStaticMethod(String name) {
        super(name);
    }

    // Attempting to override the static method 
    // This will cause a compile-time error
    /*
    @Override
    static void displayText() {
        System.out.println("Overridden DisplayText");
    }
    */

    @Override
    void display() {
        // Static variables are inherited from the parent class
        idStatic = 90;  // Access and modify the parent's static variable
        System.out.println("ID: " + idStatic + ", Name: " + name);
        super.display();  // Call the parent class's non-static method
    }

    // Correct way to use static methods from the parent class
    static void displayText() {
        StaticVariables.displayText(); // Call the parent class static method
    }

    public static void main(String[] args) {
        displayText();  // Calls the static method defined in this class
    }
}
登入後複製
  • super 關鍵字:同樣,super 關鍵字指的是父類別的實例。 由於靜態方法屬於類別而非實例,因此 super 在靜態上下文中也是無效的

例子:

static void displayText() {
    // Cannot use 'this' in a static context
    this.display();  // --> Compile-time error
}
登入後複製

附加說明:

  • 概念 1:重寫啟用依賴實例的運行時多態性。由於靜態方法不與實例關聯,因此無法覆寫它們。但是,靜態變數是繼承的並且可以在子類別中存取或修改,如範例所示。
  • 概念2:this 和 super 都引用一個實例-this 引用目前物件的成員,而 super 引用父物件的成員。由於靜態方法獨立於任何實例運行,因此 this 和 super 都不能在靜態方法或靜態區塊中使用。

4. 關鍵概念總結

  • 靜態方法可以重載:可以定義多個名稱相同但參數清單不同的靜態方法。此問題在編譯時期間解決。
  • 靜態方法不能被重寫:由於靜態方法屬於類別而不是實例,因此它們不支援運行時多態性。
  • 靜態變數是繼承的:子類別中可以存取和修改父類別的靜態變數。
  • 靜態上下文中沒有 this 或 super:由於靜態方法不會對實例進行操作,因此不能使用 this 和 super 關鍵字。

結論

在這篇文章中,我們介紹了重載和重寫靜態方法的細微差別,討論了在靜態上下文中使用this 和super 的限制,並解釋了靜態變量在繼承中的行為方式。這些概念對於理解 Java 中靜態成員與實例成員的差異至關重要。


相關貼文

  • Java 基礎

  • 陣列面試重點

  • Java 記憶體基礎

  • 集合架構重點

編碼快樂!

以上是static關鍵字:重載、重寫以及this和super的作用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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