英文:
Golang, calling parents' method
问题
从下面的示例中,有没有办法让Child对象调用Parent的方法?例如,我希望Child(boy1和girl1)调用parent的"Remember"方法,这样父母就可以记住Child希望他们记住的事情。
谢谢
package main
import "fmt"
type child struct {
Name string
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
}
type parent struct {
Name string
Children []child
Memory []string
}
func (p *parent) Yell() {
fmt.Println("Parent's yelling")
}
func (p *parent) Remember(line string) {
p.Memory = append(p.Memory, line)
}
func main() {
p := parent{}
p.Name = "Jon"
boy1 := child{}
boy1.Name = "Jon's boy"
girl1 := child{}
girl1.Name = "Jon's girl"
p.Children = append(p.Children, boy1)
p.Children = append(p.Children, girl1)
fmt.Println(p)
p.Yell()
for i := 0; i < len(p.Children); i++ {
p.Children[i].Yell()
}
}
感谢@Jim,这是解决方案。指针总是令人困惑的。
package main
import "fmt"
type child struct {
Name string
prnt *parent
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
}
type parent struct {
Name string
Children []child
Memory []string
}
func (p *parent) Yell() {
fmt.Println("Parent's yelling")
}
func (p *parent) Remember(line string) {
p.Memory = append(p.Memory, line)
}
func main() {
p := parent{}
p.Name = "Jon"
boy1 := child{}
boy1.Name = "Jon's boy"
boy1.prnt = &p
girl1 := child{}
girl1.Name = "Jon's girl"
girl1.prnt = &p
p.Children = append(p.Children, boy1)
p.Children = append(p.Children, girl1)
fmt.Println(p)
p.Yell()
for i := 0; i < len(p.Children); i++ {
p.Children[i].Yell()
p.Children[i].prnt.Remember("test:" + p.Children[i].Name)
}
fmt.Println(p.Memory)
}
英文:
From the example below, is there anyway that Child object can call Parent's method? For instance, I want Child (boy1 and girl1) to call parent's "Remember" method; so parents can remember what Child want them to remember.
Thank you so much
package main
import "fmt"
type child struct {
Name string
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
}
type parent struct {
Name string
Children []child
Memory []string
}
func (p *parent) Yell() {
fmt.Println("Parent's yelling")
}
func (p *parent) Remember(line string) {
p.Memory = append(p.Memory, line)
}
func main() {
p := parent{}
p.Name = "Jon"
boy1 := child{}
boy1.Name = "Jon's boy"
girl1 := child{}
girl1.Name = "Jon's girl"
p.Children = append(p.Children, boy1)
p.Children = append(p.Children, girl1)
fmt.Println(p)
p.Yell()
for i:=0;i<len(p.Children);i++ {
p.Children[i].Yell()
}
}
Thanks to @Jim, here's the solution. The pointer is always confusing.
package main
import "fmt"
type child struct {
Name string
prnt *parent
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
}
type parent struct {
Name string
Children []child
Memory []string
}
func (p *parent) Yell() {
fmt.Println("Parent's yelling")
}
func (p *parent) Remember(line string) {
p.Memory = append(p.Memory, line)
}
func main() {
p := parent{}
p.Name = "Jon"
boy1 := child{}
boy1.Name = "Jon's boy"
boy1.prnt = &p
girl1 := child{}
girl1.Name = "Jon's girl"
girl1.prnt = &p
p.Children = append(p.Children, boy1)
p.Children = append(p.Children, girl1)
fmt.Println(p)
p.Yell()
for i := 0; i < len(p.Children); i++ {
p.Children[i].Yell()
p.Children[i].prnt.Remember("test:" + p.Children[i].Name)
}
fmt.Println(p.Memory)
}
答案1
得分: 3
你可以在子结构体中添加一个指向父结构体的指针。
type child struct {
Name string
parent *parent
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
p.parent.Remember(p.Name + " called")
p.parent.Yell()
}
英文:
You can add a pointer to the parent in the child struct
type child struct {
Name string
parent *parent
}
func (p *child) Yell() {
fmt.Println("Child's yelling")
p.parent.Remember(p.Name + " called")
p.parent.Yell()
}
答案2
得分: 1
我只是Golang的初学者。
Golang的约定似乎是类名使用驼峰命名法,当继承一个基类("Parent")时,不应该分配一个变量名。p.Parent
会自动工作,如下所示:
type Child struct {
*Parent // 放在顶部
Name string
}
func (p *Child) Yell() {
fmt.Println("Child's yelling")
p.Parent.Remember(p.Name + " called")
p.Parent.Yell()
}
参考链接:
- http://arch-stable.blogspot.hk/2012/05/golang-call-inherited-constructor.html
- https://medium.com/@simplyianm/why-gos-structs-are-superior-to-class-based-inheritance-b661ba897c67
还有一些其他的继承示例,使用非指针的父类,如下所示:
type Child struct {
Parent // 放在顶部
Name string
}
还不确定哪种方式更官方和方便。
英文:
I am just a starter of Golang.
Seems Golang convention is CamelCase for class name, and when inherit a base class ("Parent") shouldn't assign a variable name. p.Parent
will automatically work, i.e. as follows:
type Child struct {
*Parent // put at top
Name string
}
func (p *Child) Yell() {
fmt.Println("Child's yelling")
p.Parent.Remember(p.Name + " called")
p.Parent.Yell()
}
Reference:
- http://arch-stable.blogspot.hk/2012/05/golang-call-inherited-constructor.html
- https://medium.com/@simplyianm/why-gos-structs-are-superior-to-class-based-inheritance-b661ba897c67
Some other example inherit with non-pointer Parent like:
type Child struct {
Parent // put at top
Name string
}
Not sure which one is more official and handy yet
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论