dpkpaxhzffixp8426 2019-09-08 19:54
浏览 8
已采纳

有没有一种方法可以访问结构字段或更改具有相同字段的类型(不同结构)的接口

two/more different sets of data which each data requires it is own struct for different functions, and these two/more sets of data struct share the same field. how can I combine these two set of data (different types), and can be called by another function which requires access filed from each sets of data.

package main

import "fmt"

type Plants struct {
    Name string
    Age  int
}

type Animal struct {
    Name string
    Age  int
}

type General struct {
    Name string
    Age  int
}

func (a *Animal) AnimalHealth() {
    fmt.Printf("Animal: %s is %+v years old who is in healthy condition!
", a.Name, a.Age)
}

func (p *Plants) PlantsHealth() {
    fmt.Printf("Plants: %s is %+v years old who is in healthy condition!
", p.Name, p.Age)
}

func (g *General) alive() {
    fmt.Printf("%s is %+v alive. 
", g.Name, g.Age)
}

func main() {
    dog := Animal{
        Name: "luckdog",
        Age:  6,
    }

    flower := Plants{
        Name: "sunflower",
        Age:  5,
    }

    dog.AnimalHealth()    // Output is required.
    flower.PlantsHealth()  // Output is required. 

    var all []interface{}
    all = append(all, dog, flower)
    fmt.Printf("Print out all %s
", all)

    for _, v := range all {
        fmt.Printf("This is iterate through all value %v
", v)  //Tested *Animal data and *Plants data are combined. 
//      v.alive()   // *** Output is required, how should access fields, brain is burning.  ***

    }
}

make v.alive() works.

  • 写回答

1条回答 默认 最新

  • dongrong8972 2019-09-08 20:04
    关注

    Looks like you need a common interface:

    type Animal interface {
        DoSomething()
    }
    
    type A struct {
    }
    
    type B struct {
    }
    
    func (a *A) DoSomething() {
        fmt.Println("A")
    }
    
    func (b *B) DoSomething() {
        fmt.Println("B")
    }
    
    func test(some Animal) {
        some.DoSomething()
    }
    
    func main() {
        a := &A{}
        b := &B{}
    
        var all []interface{}
        all = append(all, a, b)
    
        for _, v := range all {
            v.(Animal).DoSomething()
        }
    }
    

    Update:

    As have said @mkopriva, if you need to have common fields in both struct you can create a new struct and embed it in others:

    type Animal interface {
        DoSomething()
    }
    
    type Common struct {
        Name string
    }
    
    type A struct {
        Common 
    }
    
    type B struct {
        Common 
    }
    
    func (a *Common ) DoSomething() {
        fmt.Println(a.Name)
    }
    
    func test(some Animal) {
        some.DoSomething()
    }
    
    func main() {
        a := &A{Common{Name: "a"}}
        b := &B{Common{Name: "b"}}
    
        var all []interface{}
        all = append(all, a, b)
    
        for _, v := range all {
            v.(Animal).DoSomething()
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应