英文:
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
}
现在我想要做以下事情:
- 以某种方式将
'data DatumType'
与base
结合起来,这样通过查看base
的定义,可以知道哪些数据是所有结构体共有的。 - 在一个地方实现
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:
- Keep '
data DatumType
' withbase
in some way so that looking at the definition ofbase
one can know what data is common to all structs. - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论