Go語言程式:從另一個建構函式呼叫建構函式


在 Go 程式語言中,建構函式是一種特殊的函式,用於在物件最初建立時初始化物件的狀態。它用於初始化變數併為物件提供資料。我們將使用兩種方法執行此程式,並且在兩個示例中都將使用結構體,在第二個示例中將使用匿名結構體。讓我們透過這些示例來了解程式是如何執行的。

方法 1:使用返回所需型別的函式

在這種方法中,要構造一個新的 Child 結構體,NewChildWithAge 函式執行 NewChild 函式,設定年齡,並返回完成的結構體。

演算法

  • 步驟 1 − 建立一個名為 main 的包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 使用 Name 和 Age 屬性構造一個 Child 結構體。

  • 步驟 3 − 建立一個名為 NewChild 的函式,該函式接受一個名稱字串作為輸入並返回指向名為 Child 的結構體的指標。此函式將 Child 結構體的 Name 欄位初始化為輸入名稱,而 Age 欄位初始化為 0。

  • 步驟 4 − 建立一個名為 NewChildWithAge 的函式,該函式接受一個名稱字串和一個年齡整數作為輸入並返回指向名為 Child 的結構體的指標。

  • 步驟 5 − 在 NewChildWithAge 函式中呼叫 NewChild 函式以建立一個新的 Child 結構體。

  • 步驟 6 − 將年齡輸入設定為 NewChild 返回的 Child 結構體的 Age 欄位。

  • 步驟 7 − 返回 Child 結構體並呼叫 main 函式中的 NewChildWithAge 函式,傳遞引數“Virat”和 10 作為輸入。

  • 步驟 8 − 在名為 c 的變數中儲存生成的 Child 結構體。

  • 步驟 9 − 使用 fmt.Println() 函式列印 Child 結構體的 Name 和 Age 欄位,其中 ln 表示換行。

示例

在此示例中,我們將使用返回所需型別的函式。讓我們看一下程式碼。

package main
import "fmt"

type Child struct {
   Name string
   Age int
}

func NewChild(name string) *Child {
   return &Child{   
      Name: name,
      Age:  0,
   }
}

func NewChildWithAge(name string, age int) *Child {
   c := NewChild(name)     //call this function to create a new child struct
   c.Age = age
   return c  //return the child attributes
}

func main() {
   c := NewChildWithAge("Virat", 10)
   fmt.Println("Here, calling one constructor from another can be represented as:")
   fmt.Println(c.Name, c.Age)  //print he name and age of child
}

輸出

Here, calling one constructor from another can be represented as:
Virat 10

方法 2:使用匿名結構體欄位

此方法具有 Child 型別的匿名欄位,允許它繼承 Child 的所有屬性。透過使用 NewChild 函式初始化 Child 欄位並將 Age 欄位設定為提供的年齡,NewChildWithAge 方法構造一個新的 ChildWithAge 結構體。

演算法

  • 步驟 1 − 建立一個名為 main 的包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 只使用 Name 欄位建立 Child 結構體。

  • 步驟 3 − 建立一個名為 ChildWithAge 的結構體,它有兩個欄位:一個 Age 欄位和一個匿名的 Person 欄位。

  • 步驟 4 − 建立一個名為 NewChild 的函式,該函式接受一個名稱字串作為輸入並返回指向名為 Child 的結構體的指標。此函式使用提供的名稱初始化 Child 結構體的 Name 欄位。

  • 步驟 5 − 建立一個名為 NewChildWithAge 的函式,該函式接受一個名稱字串和一個年齡整數作為輸入並返回指向名為 ChildWithAge 的結構體的指標。

  • 步驟 6 − 在 NewChildWithAge 函式中初始化一個新的 ChildWithAge 結構體。

  • 步驟 7 − 使用 NewChild 函式初始化 ChildWithAge 結構體的匿名 Person 欄位。

  • 步驟 8 − 在 ChildWithAge 結構體的 Age 欄位中設定輸入年齡。

  • 步驟 9 − 將 ChildWithAge 結構體返回到函式,並在 main 函式中呼叫 NewChildWithAge 函式,傳遞引數“Virat”和 10 作為輸入。

  • 步驟 10 − 在名為 c 的變數中儲存生成的 ChildWithAge 結構體。

  • 步驟 11 − 列印 ChildWithAge 結構體的 Name 和 Age 屬性。

示例

在此示例中,我們將使用匿名結構體欄位從另一個建構函式呼叫建構函式。讓我們看一下程式碼。

package main
import "fmt"

type Child struct {
   Name string
   Age  int
}

type ChildWithAge struct {
   Child
   Age int
}

func NewChild(name string) *Child {
   return &Child{
      Name: name,
   }
}

func NewChildWithAge(name string, age int) *ChildWithAge {
   return &ChildWithAge{
      Child: *NewChild(name),  //call the function to create a child struct
      Age:   age,
   }
}

func main() {
   c := NewChildWithAge("Virat", 10)  //set the values and store the output in c
   fmt.Println("Here, calling one constructor from another can be represented as:")
   fmt.Println(c.Name, c.Age) //print the name and age of child
}

輸出

Here, calling one constructor from another can be represented as:
Virat 10

結論

我們使用兩種方法透過從另一個建構函式呼叫建構函式來執行程式。在第一種方法中,我們使用了返回所需型別的函式,在第二種方法中,我們使用了匿名結構體來執行程式。

更新時間: 2023年2月20日

734 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告