doumeng1089 2017-08-31 06:14
浏览 147
已采纳

如何在Golang中调用父方法并传递任何将父结构扩展为参数的子代

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条回答 默认 最新

  • dongyan8929 2017-08-31 08:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示