CodeGym /Java Blog /Toto sisi /Java 中的構造函數鏈接
John Squirrels
等級 41
San Francisco

Java 中的構造函數鏈接

在 Toto sisi 群組發布

什麼是構造函數鏈?

Java中的構造函數是在類的對象創建中使用的特定方法。每次創建類的對象時都會調用構造函數。它可用於在創建時為對象的屬性賦值。一個 Java 類中可以有多個具有不同參數列表的構造函數。構造函數鏈接用於在對象創建時調用同一類/父類的構造函數的不同實現。

構造函數鍊是如何在 Java 中實現的?

根據如何調用構造函數,有兩種鏈接構造函數的方法。它們如下。
  • 使用this()關鍵字——調用同一類的構造函數
  • 使用super()關鍵字——調用父類的構造函數
這在以下示例中進行了解釋。Java 中的構造函數鏈接 - 1

構造函數鏈接示例 #1 – 構造函數使用 this() 關鍵字鏈接

我們為 DerivedClass 聲明了四個構造函數。一個沒有參數,另外三個有不同的參數。在每個構造函數中,this()關鍵字用於調用同一類的下一個構造函數。

package com.tutorialwriting.constchaining;
 
public class DerivedClass{
 
    String firstName;
    String country;
    int age;
 
    public DerivedClass() {
        // calling one argument constructor
        this("Maggie");
    }
 
    public DerivedClass(String firstName) {
        // calling two argument constructor
        this(firstName, 15);
    }
 
    public DerivedClass(String firstName, int age) {
        // calling three argument constructor
        this(firstName, age, "Australia");
    }
 
    public DerivedClass(String firstName, int age, String country) {
        this.firstName = firstName;
        this.age = age;
        this.country = country;
    }
 
    void displayValues() {
        System.out.println("First Name : " + firstName);
        System.out.println("Country : " + country);
        System.out.println("Age : " + age);
    }
 
    public static void main(String args[]) {
        DerivedClass object = new DerivedClass();
        object.displayValues();
    }
}
執行的輸出Java 中的構造函數鏈接 - 2

構造函數鏈接示例 #2 – 構造函數使用 super() 關鍵字鏈接

在這裡,子類使用super()關鍵字調用父類的構造函數。BaseClass 具有三個構造函數。沒有參數的構造函數使用this()調用 BaseClass 的三參數構造函數之一。

package com.tutorialwriting.constchaining;
 
public class BaseClass {
 
    public BaseClass() {
        //calling a three argument constructor of the same class
        this("Male", "English", "1989/11/10");
        System.out.println("I'm executed third!!!");
    }
 
    public BaseClass(String firstName, String surname, int idNo) {
        System.out.println("I'm executed first!");
        System.out.println("First name : " + firstName);
        System.out.println("Surname : " + surname);
        System.out.println("ID Number : " + idNo);
    }
 
    public BaseClass(String gender, String nationality, String birthDate) {
        System.out.println("I'm executed second!!");
        System.out.println("Gender : " + gender);
        System.out.println("Nationality : " + nationality);
        System.out.println("Birth Date : " + birthDate);
    }
 
}
DerivedClass 有兩個構造函數,每個構造函數都使用super()調用超類的不同構造函數。

package com.tutorialwriting.constchaining;
 
public class DerivedClass extends BaseClass {
 
    public DerivedClass() {
        //calling no argument constructor of the super class
        super();
    }
 
    public DerivedClass(String firstName, String surname, int idNo) {
        //calling three argument constructor of the super class
        super(firstName, surname, idNo);
    }
 
    public static void main(String args[]) {
        DerivedClass object2 = new DerivedClass("Paul", "Wilson", 123456);
        DerivedClass object1 = new DerivedClass();
    }
}
執行的輸出Java 中的構造函數鏈接 - 3

隱式與顯式構造函數調用

Java 有兩種不同的調用構造函數的方式:隱式調用和顯式調用。
  • 顯式調用是指在代碼中使用this()super()顯式調用構造函數。
  • 隱式調用是指在子類構造函數沒有顯式調用的情況下隱式調用超類的無參數構造函數。換句話說,如果程序員明確不在代碼中調用super() ,編譯器會將super()調用添加為子類的任何構造函數的第一行。

為什麼我們需要構造函數鏈接?

在 Java 中使用構造函數鏈有幾個不同的目的,如下所列。
  • 它是一種訪問其他構造函數的屬性或父類的屬性的方法。
  • 在調用其他構造函數時,只使用了一個對象,即該類的當前實例。初始化發生在一個地方,但我們有特權通過鏈調用不同的構造函數實現。這對內存管理和代碼維護有很大幫助。

結論

在本教程中,我們討論了 Java 中的構造函數鏈。構造函數是在創建對象時調用的類方法代碼段。一個 Java 類可以有任意數量的具有不同參數列表的構造函數。構造函數鏈接是一種使用類的一個實例處理不同初始化的簡便方法。下面列出了本教程中需要注意的一些要點。
  • 如果程序員沒有明確地將其添加到代碼中,編譯器將向 Java 類添加一個公共的無參數構造函數。這稱為默認構造函數。
  • this()super()應該寫在構造函數的第一行。
  • this()用於調用同一類的構造函數,而super()用於調用直接超類的構造函數。
  • 類中至少應該有一個不包含this()關鍵字的構造函數。
  • 如果未明確添加,編譯器會向每個子類構造函數添加一個無參數super()調用。這將有助於正確實例化類。
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION