如何调用父方法并传递在Golang中扩展父结构作为参数的任何子项 [英] How to call parent method and pass any child that extending the parent struct as argument in Golang

查看:238
本文介绍了如何调用父方法并传递在Golang中扩展父结构作为参数的任何子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在学习Golang,我想问一些问题。
是否可以做这样的事情,并将任何其他孩子传递给扩展父结构的PM方法?

  type Parent struct {
PAttribute string
}

func(p * Parent)PMethod(c * Child){
fmt.Println(this is parent Attribute:+ p.PAttribute)
fmt.Println(this is child属性:+ c.CAttribute)
}

类型子结构{

CAttribute字符串
}

类型Child2结构{
父类
CAttribute字符串
}


func main( ){
c:=孩子{
父母{
父母
},
孩子,
}
c.PM方法& c)

c2:= Child2 {
父母{
父母
},
孩子,
}
c2.PMethod(& c2)
}

谢谢

解决方案

正如其他人所说,f关于继承, Go没有继承有很好的理由。所以,不要在父母和孩子方面考虑。 Go有作文(你在这里使用),但它的行为有所不同。您可以在Go中使用接口以传入Child和Child2并让接收者无论它是哪一个(只要它具有相同的功能)都是无关紧要的。但是这可能是一个错误,因为您试图重新创建Go中熟悉的继承。不要这样做。

如果你的类型是这样的相关的,你应该从一个嵌入类接受一个类的实例来修改。嵌入式类应该在自己的领域中行动,就是这样 - 这会将关注点分隔开来,并将数据与依靠它的方法保持一致。因此,不要考虑家长和孩子,而是考虑一些想要分享的代码/数据。



考虑经典继承问题动物 - 与猫和狗。通过继承,你将拥有一只猫和一只狗以及一个抽象基类动物。动物可能有你期望的所有方法,然后你可以在Animal上写一个Say()方法,该方法什么都不说,Say()哪个woofs和Say()哪些Meows on Dogs and Cats。

在Go中,您只需在每个方法上使用Cats and Dogs和一个不同的Say()方法,这两个方法都符合其他人定义的Speaker()接口。为了分享一些行为,需要重复一些额外的代码和复杂性。



请务必阅读 Effective Go 并尝试使用可用的工具,而不是像您熟悉的继承一样重新创建工具。

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

解决方案

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.

这篇关于如何调用父方法并传递在Golang中扩展父结构作为参数的任何子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆