ds9567 2019-09-23 19:30
浏览 49
已采纳

如何对变量使用多态[重复]

This question already has an answer here:

I am new in Go and started learning about polymorphism.
I know how to do it when multiple objects need to use the same function.
But I have a new problem, I don't know what to do in a case I have the same variable on different objects.

In the following example I have two different objects: struct1 and struct2. Both of them have the same variable name.
I can run over them and check which is which and work accordingly (you can test it here):

package main

import (
    "fmt"
)
type struct1 struct {
    name string
}

type struct2 struct {
    name string
}

func main(){
    structMap := make(map[string]interface{})
    s1 := struct1{name:"struct1_name"}
    s2 := struct2{name:"struct2_name"}
    structMap["struct1"] = s1
    structMap["struct2"] = s2

    for key, _ := range structMap {
        switch key {
            case "struct1":
                generic := structMap[key].(struct1)
                fmt.Println(generic.name)
            case "struct2":
                generic := structMap[key].(struct2)
                fmt.Println(generic.name)
        }
    }
}

But if I had 20 objects ? I would need to do 20 checks?
So I wonder if it possible to do an interface with variables, something like:

type genericStruct interfcae {
   name string
}

...
for key, _ := range structMap {
    generic := structMap[key].(genericStruct)
    fmt.Println(generic.name)

}

Of course this code doesn't work because I don't know how to do it, but I wanted to know about a way to do it.

EDIT:
I tried to use interface based on the example from: https://gobyexample.com/interfaces
Thanks for Robbie Milejczak and the other guys who help me.

This the new working code:

package main

import (
    "fmt"
)
type struct1 struct {
    name string
}

type struct2 struct {
    name string
}

type genericInterface interface {
    GetName() string
}

func (r struct1 ) GetName() string{
    return r.name
}

func (r struct2 ) GetName() string{
    return r.name
}

func printName(g interface{}){
    a := g.(genericInterface)
    fmt.Println(a.GetName())
}

func main(){
    structMap := make(map[string]interface{})
    s1 := struct1{name:"struct1_name"}
    s2 := struct2{name:"struct2_name"}
    structMap["struct1"] = s1
    structMap["struct2"] = s2

    for key, _ := range structMap {
    printName(structMap[key])
    }
}
</div>
  • 写回答

1条回答 默认 最新

  • douchan0523 2019-09-23 19:51
    关注

    For this contrived example you could use getters / setters instead of static properties to leverage interfaces:

    type GenericStruct interface {
       GetName() string
    }
    

    And now any struct with a receiver called GetName will satisfy the GenericStruct interface:

    type MyStruct struct {
      Name string
    }
    
    func (ms *MyStruct) GetName() string {
      return ms.Name
    }
    

    This could get messy with more complex structs, in which case you may want to consider the composition suggestion (typically achieved via embedding) or a third party library such as genny

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

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题