Golang 调用一个构造函数从另一个构造函数

Golang 调用一个构造函数从另一个构造函数

在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 - 使用两个字段(一个Age字段和一个匿名的Person字段)创建ChildWithAge结构体。

  • 步骤4 - 创建一个名为NewChild的函数,该函数接受一个名为name的字符串作为输入,返回一个指向名为Child的结构体的指针。该函数使用提供的name来初始化Child结构体的Name字段。

  • 步骤5 - 创建一个名为NewChildWithAge的函数,该函数接受一个名为name的字符串和一个名为age的整数作为输入,返回一个指向名为ChildWithAge的结构体的指针。

  • 步骤6 - 在NewChildWithAge函数中初始化一个新的ChildWithAge结构体。

  • 步骤7 - 使用NewChild函数来初始化ChildWithAge结构体的匿名Person字段。

  • 步骤8 - 将输入的age设置在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

结论

我们通过使用两种方法中的一种调用一个构造函数来执行程序。在第一种方法中,我们使用了一个返回所需类型的函数,而在第二种方法中,我们使用了一个匿名结构体来执行程序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程