dr5779 2016-06-30 13:00
浏览 158
已采纳

Golang方法覆盖

I have the following:

type Base struct {

}

func(base *Base) Get() string {
    return "base"
}

func(base *Base) GetName() string {
    return base.Get()
}

I want to define a new type with a new implementation of Get() so that I can use the new type in place of Base and where GetName() is called it calls the new Get() implementation . If I were using Java I would inherit Base and override Get(). How should I achieve this in Go? I want to avoid breaking changes if possible, so existing consumers of Base do not need to be changed.

My first stab at this looks like..

type Sub struct {
    Base
}

func(sub *Sub) Get() string {
    return "Sub"
}

..which doesn't work. My brain isn't wired for Go yet clearly.

  • 写回答

2条回答 默认 最新

  • dounouxi1020 2016-06-30 13:35
    关注

    Go is not a "classic" OO language: it doesn't know the concept of classes and inheritance.

    However it does contain the very flexible concept of interfaces, with which a lot of aspects of object-orientation can be made available. Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here.

    An interface defines a set of methods, but these methods do not contain code: they are not implemented (this means they are abstract).

    So the way you can achieve to use different type inside the same method is to use interfaces.

    Here is simple example to prove it:

    package main
    
    import (
        "fmt"
    )
    
    type Base struct{}
    type Baser interface {
        Get() float32
    }
    
    type TypeOne struct {
        value float32
        Base
    }
    
    type TypeTwo struct {
        value float32
        Base
    }
    
    type TypeThree struct {
        value float32
        Base
    }
    
    func (t *TypeOne) Get() float32 {
        return t.value
    }
    
    func (t *TypeTwo) Get() float32 {
        return t.value * t.value
    }
    
    func (t *TypeThree) Get() float32 {
        return t.value + t.value
    }
    
    func main() {
        base := Base{}
        t1 := &TypeOne{1, base}
        t2 := &TypeTwo{2, base}
        t3 := &TypeThree{4, base}
    
        bases := []Baser{Baser(t1), Baser(t2), Baser(t3)}
    
        for s, _ := range bases {
            switch bases[s].(type) {
            case *TypeOne:
                fmt.Println("TypeOne")
            case *TypeTwo:
                fmt.Println("TypeTwo")
            case *TypeThree:
                fmt.Println("TypeThree")
            }
    
            fmt.Printf("The value is:  %f
    ", bases[s].Get())
        }   
    }
    

    Go Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀