Golang Method Override

后端 未结 2 1138
耶瑟儿~
耶瑟儿~ 2021-01-17 17:20

I have the following:

type Base struct {

}

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

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


        
相关标签:
2条回答
  • 2021-01-17 17:41

    Interfaces are named collections of method signatures:
    see: https://gobyexample.com/interfaces
    and: http://www.golangbootcamp.com/book/interfaces
    so it is better not to use in such OOP way.


    what you asking is not Golang idiomatic but possible (2 ways):

    this is what you need (working sample code):

    package main
    
    import "fmt"
    
    type Base struct {
    }
    
    func (base *Base) Get() string {
        return "base"
    }
    
    type Getter interface {
        Get() string
    }
    
    func (base *Base) GetName(getter Getter) string {
        if g, ok := getter.(Getter); ok {
            return g.Get()
        } else {
            return base.Get()
        }
    }
    
    // user code :
    type Sub struct {
        Base
    }
    
    func (t *Sub) Get() string {
        return "Sub"
    }
    func (t *Sub) GetName() string {
        return t.Base.GetName(t)
    }
    
    func main() {
        userType := Sub{}
        fmt.Println(userType.GetName()) // user string
    }
    

    output:

    Sub
    

    as you see one initialization code must be done in user code:

    func (t *Sub) GetName() string {
        return t.Base.GetName(t)
    }
    

    also this works:

    package main
    
    import "fmt"
    
    type Getter interface {
        Get() string
    }
    
    type Base struct {
        Getter
    }
    
    func (base *Base) Get() string {
        return "base"
    }
    
    func (base *Base) GetName() string {
        return base.Getter.Get()
    }
    
    // user code :
    type Sub struct {
        Base
    }
    
    func (t *Sub) Get() string {
        return "Sub"
    }
    func New() *Sub {
        userType := &Sub{}
        userType.Getter = interface{}(userType).(Getter)
        return userType
    }
    
    func main() {
        userType := New()
        fmt.Println(userType.GetName()) // user string
    }
    

    output:

    Sub
    

    as you see one initialization code must be done in user code:

    userType.Getter = interface{}(userType).(Getter)
    
    0 讨论(0)
  • 2021-01-17 17:43

    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\n", bases[s].Get())
        }   
    }
    

    Go Playground

    0 讨论(0)
提交回复
热议问题