How to call parent method and pass any child that extending the parent struct as argument in Golang

穿精又带淫゛_ 提交于 2019-12-23 06:21:32

问题


I'm still learning Golang and I want to ask something. Is it possible to do something like this and passing any other child to PMethod that extending Parent struct?

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)
}

Thank you


回答1:


As others have said, forget about inheritance, Go doesn't have inheritance for good reasons. So stop thinking in terms of parents and children. Go has composition (which you're using here), but it behaves differently. You could use interfaces in Go in order to pass in Child and Child2 and have the receiver indifferent as to which one it gets (as long as it has the same functions). BUT this is probably a mistake as you're trying to recreate the inheritance you are familiar with in Go. Don't do that.

If your types are related like this, you should probably take a step back from an embedded class accepting instances of a class to modify. The embedded class should act upon its own fields, and that's it - this compartmentalizes concerns and keeps data with the methods that act on it. So instead of thinking about Parents and Children, think about a bit of code/data you want to share.

Consider the classic inheritance problem Animals - with cats and dogs. With inheritance you'd have a Cat and a Dog and an abstract base class Animal. Animal might have all the methods you expect then you write perhaps a Say() method on Animal which says nothing and Say() which woofs and Say() which Meows on Dogs and Cats.

In Go you'd simply have Cats and Dogs and a different Say() method on each which both conform to a Speaker() interface someone else defines. Prefer a bit of duplication to lots of extra code and complexity just to share a little behaviour.

Be sure to read Effective Go and try to use the tools available, rather than recreating tools like inheritance you're familiar with.



来源:https://stackoverflow.com/questions/45974464/how-to-call-parent-method-and-pass-any-child-that-extending-the-parent-struct-as

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!