Golang,调用父类的方法

huangapple go评论89阅读模式
英文:

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 &quot;fmt&quot;

type child struct {
	Name string 
}

func (p *child) Yell() {
	fmt.Println(&quot;Child&#39;s yelling&quot;)
}

type parent struct {
	Name string 
	Children []child
	Memory []string
}

func (p *parent) Yell() {
	fmt.Println(&quot;Parent&#39;s yelling&quot;)
}

func (p *parent) Remember(line string) {
	p.Memory = append(p.Memory, line)
}

func main() {
	p := parent{}
	p.Name = &quot;Jon&quot;
	boy1 := child{}
	boy1.Name = &quot;Jon&#39;s boy&quot;
	girl1 := child{}
	girl1.Name = &quot;Jon&#39;s girl&quot;
	p.Children = append(p.Children, boy1)
	p.Children = append(p.Children, girl1)
	fmt.Println(p)
	
	p.Yell()
	for i:=0;i&lt;len(p.Children);i++ {
		p.Children[i].Yell()
	}
}

Thanks to @Jim, here's the solution. The pointer is always confusing.

package main

import &quot;fmt&quot;

type child struct {
	Name string
	prnt *parent
}

func (p *child) Yell() {
	fmt.Println(&quot;Child&#39;s yelling&quot;)
}

type parent struct {
	Name     string
	Children []child
	Memory   []string
}

func (p *parent) Yell() {
	fmt.Println(&quot;Parent&#39;s yelling&quot;)
}

func (p *parent) Remember(line string) {
	p.Memory = append(p.Memory, line)
}

func main() {
	p := parent{}
	p.Name = &quot;Jon&quot;
	boy1 := child{}
	boy1.Name = &quot;Jon&#39;s boy&quot;
	boy1.prnt = &amp;p
	girl1 := child{}
	girl1.Name = &quot;Jon&#39;s girl&quot;
	girl1.prnt = &amp;p

	p.Children = append(p.Children, boy1)
	p.Children = append(p.Children, girl1)
	fmt.Println(p)

	p.Yell()
	for i := 0; i &lt; len(p.Children); i++ {
		p.Children[i].Yell()
		p.Children[i].prnt.Remember(&quot;test:&quot; + 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(&quot;Child&#39;s yelling&quot;)
	p.parent.Remember(p.Name + &quot; called&quot;)
	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()
}

参考链接:

  1. http://arch-stable.blogspot.hk/2012/05/golang-call-inherited-constructor.html
  2. 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(&quot;Child&#39;s yelling&quot;)
    p.Parent.Remember(p.Name + &quot; called&quot;)
    p.Parent.Yell()
}

Reference:

  1. http://arch-stable.blogspot.hk/2012/05/golang-call-inherited-constructor.html
  2. 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

huangapple
  • 本文由 发表于 2016年3月5日 03:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/35804991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定