我还在学习Golang,想请教一些事情。是否可以执行类似的操作并将任何其他子级传递给扩展 Parent 结构的 PMethod ?
type Parent struct{
PAttribute string
}
func (p *Parent) PMethod(c *Child){
fmt.Println("this is parent Attribute : " + p.PAttribute)
fmt.Println("this is child Attribute : " + c.CAttribute)
}
type Child struct{
Parent
CAttribute string
}
type Child2 struct{
Parent
CAttribute string
}
func main(){
c := Child{
Parent{
"parent"
},
"child",
}
c.PMethod(&c)
c2 := Child2{
Parent{
"parent"
},
"child",
}
c2.PMethod(&c2)
}
Run Code Online (Sandbox Code Playgroud)
谢谢