Go语言中使用接口实现继承的等效方式,包括数据和成员函数。

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

GoLang equivalent for inheritence using interface with data and member functions

问题

我是一名 Golang 新手,如果有什么显而易见的地方我忽略了,请原谅。我有以下的结构体:

type base interface {
    func1()
    func2()
    common_func()
}

type derived1 struct {
    base // 匿名字段,表示继承
    data DatumType
}

type derived2 struct {
    base // 匿名字段,表示继承
    data DatumType
}

现在我想要做以下事情:

  1. 以某种方式将 'data DatumType'base 结合起来,这样通过查看 base 的定义,可以知道哪些数据是所有结构体共有的。
  2. 在一个地方实现 common_func(),这样派生结构体就不需要自己实现了。

我尝试使用接口来实现这个函数,但编译失败了。我尝试创建一个结构体并从中继承,但找不到好的方法来实现。是否有一种简洁的方法可以解决这个问题?

英文:

I am a golang newbie so pardon me if there is something obvious I am missing. I have the following structures:

type base interface {
    func1()
    func2()
    common_func()
}

type derived1 struct {
    base // anonymous meaning inheritence
    data DatumType
}

type derived2 struct {
    base // anonymous meaning inheritence
    data DatumType
}

Now I want to do the following:

  1. Keep 'data DatumType' with base in some way so that looking at the definition of base one can know what data is common to all structs.
  2. Implement common_func() in one place so that the derived structs don't need to do it.

I tried implementing the function with the interface but it fails compilation. I tried to create a struct and inherit from that but am not finding good ways to do that. Is there some clean way out ?

答案1

得分: 3

Go语言没有继承的概念,而是提供了嵌入的概念。

在这种情况下,你不需要将base定义为一个接口,而是将其定义为一个结构体,并将函数定义为方法。然后在派生的结构体中嵌入base,这样它们就会继承这些方法。

type base struct{
    data DatumType     
}
func (b base) func1(){

}

func (b base) func2(){

}

func (b base) common_func(){

}

type derived1 struct {
    base // 匿名嵌入,意味着继承
}

type derived2 struct {
    base // 匿名嵌入,意味着继承
}

现在你可以这样使用:

d := derived1{}
d.func1()
d.func2()
d.common_func()

并且(正如David Budworth在评论中指出的),你可以通过引用类型名称来访问base的字段,例如:

d.base.data = something
英文:

Go does not have inheritance. Instead it offers the concept of embedding.

In this case you don't need/want to define base as an interface. Make it a struct and define the functions as methods. Then embedding base in your derived structs will give them those methods.

type base struct{
    data DatumType     
}
func (b base) func1(){

}

func (b base) func2(){

}

func (b base) common_func(){

}

type derived1 struct {
    base // anonymous meaning embedding
}

type derived2 struct {
    base // anonymous meaning embedding
}

Now you can do:

d := derived1{}
d.func1()
d.func2()
d.common_func()

And (as pointed out by David Budworth in the comment) you can access the fields of base by referring to it's type name like so:

d.base.data = something

答案2

得分: 2

在Go语言中没有继承的概念,可以使用组合来实现:

type common struct {
    data DatumType
}

func (c *common) commonFunc() {
    // 使用data进行一些操作。
}

type derived1 struct {
    common
    otherField1 int
}

// 在derived1上实现其他方法。

type derived2 struct {
    common
    otherField2 string
}

// 在derived2上实现其他方法。

以上是使用组合来实现继承的示例代码。

英文:

There is no inheritance in Go. Use composition:

type common struct {
    data DatumType
}

func (c *common) commonFunc() {
    // Do something with data.
}

type derived1 struct {
    common
    otherField1 int
}

// Implement other methods on derived1.

type derived2 struct {
    common
    otherField2 string
}

// Implement other methods on derived2.

huangapple
  • 本文由 发表于 2015年6月13日 16:24:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/30816630.html
匿名

发表评论

匿名网友

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

确定