在 Go 结构中,提升方法的工作就像提升字段一样。我们在嵌套结构中使用这个概念,其中一个结构是另一个结构中的字段,只需将结构的名称添加到另一个结构中,它的行为就像嵌套结构的匿名字段。并且该结构(嵌套结构除外)的方法是嵌套结构的一部分,这种类型的方法称为提升方法。或者换句话说,提示方法是那些由子结构实现的方法,并且可以被父结构访问。
要点:
- 如果子结构和父结构包含具有相同名称但接收器类型不同的方法,则这两种方法都可以在父结构中使用,如示例 2 所示。这里的子结构和父结构都包含相同名称的方法。
- 如果子结构包含两个具有相同名称和相同接收器的方法,则这些方法不会在父结构中提升,如果您尝试这样做,则编译器将给出错误。
示例 1:
// Golang program to illustrate the
// concept of the promoted methods
package main
import "fmt"
// Structure
type details struct {
// Fields of the
// details structure
name string
age int
gender string
psalary int
}
// Nested structure
type employee struct {
post string
eid int
details
}
// Method
func (d details) promotmethod(tsalary int) int {
return d.psalary * tsalary
}
func main() {
// Initializing the fields of
// the employee structure
values := employee{
post: "HR",
eid: 4567,
details: details{
name: "Sumit",
age: 28,
gender: "Male",
psalary: 890,
},
}
// Promoted fields of the
// employee structure
fmt.Println("Name: ", values.name)
fmt.Println("Age: ", values.age)
fmt.Println("Gender: ", values.gender)
fmt.Println("Per day salary: ", values.psalary)
// Promoted method of the
// employee structure
fmt.Println("Total Salary: ", values.promotmethod(30))
// Normal fields of
// the employee structure
fmt.Println("Post: ", values.post)
fmt.Println("Employee id: ", values.eid)
}
输出:
Name: Sumit
Age: 28
Gender: Male
Per day salary: 890
Total Salary: 26700
Post: HR
Employee id: 4567
示例 2:
// Golang program to illustrate the
// concept of the promoted methods
package main
import "fmt"
// Structure
type details struct {
// Fields of the
// details structure
name string
age int
gender string
psalary int
}
// Method 1
func (e employee) promotmethod(tarticle int) int {
return e.particle * tarticle
}
// Nested structure
type employee struct {
post string
particle int
eid int
details
}
// Method 2
func (d details) promotmethod(tsalary int) int {
return d.psalary * tsalary
}
// Main method
func main() {
// Initializing the fields of
// the employee structure
values := employee{
post: "HR",
eid: 4567,
particle: 5,
details: details{
name: "Sumit",
age: 28,
gender: "Male",
psalary: 890,
},
}
// Promoted fields of
// the employee structure
fmt.Println("Name: ", values.name)
fmt.Println("Age: ", values.age)
fmt.Println("Gender: ", values.gender)
fmt.Println("Per day salary: ", values.psalary)
// Promoted method of
// the employee structure
fmt.Println("Total Salary: ", values.promotmethod(30))
// Normal fields of
// the employee structure
fmt.Println("Post: ", values.post)
fmt.Println("Employee id: ", values.eid)
fmt.Println("Total Articles: ", values.promotmethod(30))
}
输出:
Name: Sumit
Age: 28
Gender: Male
Per day salary: 890
Total Salary: 150
Post: HR
Employee id: 4567
Total Articles: 150