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
结论
我们使用两种方法通过从另一个构造函数调用构造函数来执行程序。在第一种方法中,我们使用了返回所需类型的函数,在第二种方法中,我们使用了匿名结构体来执行程序。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP