duanchi4184 2017-08-05 07:59
浏览 35
已采纳

指向方法args中的接口的指针?

Gist with code

How can I use interface Herbivore instead *Mouse in line 30?

I want to pass to method eatingVictim different structs which implements Herbivore interface, not only Mouse

  • 写回答

2条回答 默认 最新

  • dongyong5255 2017-08-05 08:57
    关注

    Let me explain:

    First of all in this function:

    func (predator Cat) eatingVictim(victim *Mouse) {
        fmt.Println(predator.name + "'s eating victim " + victim.name)
        predator.hungry = false
        victim.alive = false
    }
    

    You want to pass Herbivore. This is a bad solution because you will not use methods from Herbivore interface here. Better approach is to define another interface.

    type Animal interface {
        GetName() string
        SetAlive(bool)
    }
    

    And implement it for Mouse (and Cat if you wish):

    func (m *Mouse) GetName() string {
        return m.name
    }
    
    func (m *Mouse) SetAlive(alive bool) {
        m.alive = alive
    }
    

    Then change interface Predator to:

    type Predator interface {
        eatingVictim(victim Animal)
    }
    

    and implement it for Cat

    func (predator *Cat) eatingVictim(victim Animal) {
        fmt.Println(predator.name + "'s eating victim " + victim.GetName())
        predator.hungry = false
        victim.SetAlive(false)
    }
    

    I need to mention that if you want your original structs to be modified then you need to pass a pointer to struct, not a struct as receiver argument:

    Here Mouse struct will not be modified. Only a copy of it.

    func (herbivore Mouse) eatingGrass() {
        fmt.Println(herbivore.name + "'s eating a grass.. ^___^")
        herbivore.hungry = false
    }
    

    And here is the fixed version:

    func (herbivore *Mouse) eatingGrass() {
        fmt.Println(herbivore.name + "'s eating a grass.. ^___^")
        herbivore.hungry = false
    }
    

    If you want better explanation for this - then visit my blog post

    The last thing - as best practice - if you used Struct pointer in one of the methods of your type then all of them should take a pointer to it.

    Final solution:

    package main
    
    import "fmt"
    
    type Predator interface {
        eatingVictim(victim Animal)
    }
    
    type Herbivore interface {
        eatingGrass()
    }
    
    type Animal interface {
        GetName() string
        SetAlive(bool)
    }
    
    type Cat struct {
        name   string
        hungry bool
        alive  bool
    }
    
    type Mouse struct {
        name   string
        hungry bool
        alive  bool
    }
    
    func (herbivore *Mouse) eatingGrass() {
        fmt.Println(herbivore.name + "'s eating a grass.. ^___^")
        herbivore.hungry = false
    }
    
    func (m *Mouse) GetName() string {
        return m.name
    }
    
    func (m *Mouse) SetAlive(alive bool) {
        m.alive = alive
    }
    
    func (predator *Cat) eatingVictim(victim Animal) {
        fmt.Println(predator.name + "'s eating victim " + victim.GetName())
        predator.hungry = false
        victim.SetAlive(false)
    }
    
    func main() {
        cat := Cat{"cat", true, true}
        mouse := Mouse{"mouse", true, true}
    
        fmt.Println(cat)
        fmt.Println(mouse)
    
        mouse.eatingGrass()
        cat.eatingVictim(&mouse)
    
        fmt.Println(cat)
        fmt.Println(mouse)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀