duandong7980 2017-11-06 16:15
浏览 37

去继承和多态

This code does not run correctly :

package main
import "fmt"

type Human interface {
    myStereotype() string
}

type Man struct {
}

func (m Man) myStereotype() string {
    return "I'm going fishing."
}

type Woman struct {
}

func (m Woman) myStereotype() string {
    return "I'm going shopping."
}
func main() {
    var m *Man
    m = new (Man)
    w := new (Woman)

    var hArr []*Human

    hArr = append(hArr, m)
    hArr = append(hArr, w)

    for n, _ := range (hArr) {

        fmt.Println("I'm a human, and my stereotype is: ",
                hArr[n].myStereotype())
    }
}

It exists with :

tmp/sandbox637505301/main.go:29:18: cannot use m (type *Man) as type *Human in append:
*Human is pointer to interface, not interface
tmp/sandbox637505301/main.go:30:18: cannot use w (type *Woman) as type *Human in append:
*Human is pointer to interface, not interface
tmp/sandbox637505301/main.go:36:67: hArr[n].myStereotype undefined (type *Human is pointer to interface, not interface)

But this one runs correctly (var hArr []*Human is rewritten into var hArr []Human) :

package main
import "fmt"

type Human interface {
    myStereotype() string
}

type Man struct {
}

func (m Man) myStereotype() string {
    return "I'm going fishing."
}

type Woman struct {
}

func (m Woman) myStereotype() string {
    return "I'm going shopping."
}
func main() {
    var m *Man
    m = new (Man)
    w := new (Woman)

    var hArr []Human // <== !!!!!! CHANGED HERE !!!!!!

    hArr = append(hArr, m)
    hArr = append(hArr, w)

    for n, _ := range (hArr) {

        fmt.Println("I'm a human, and my stereotype is: ",
                hArr[n].myStereotype())
    }
}

Output is ok:

I'm a human, and my stereotype is:  I'm going fishing.
I'm a human, and my stereotype is:  I'm going shopping.

And I do not understand why. As m and w are pointers, why when I define hArr as an array of pointers on Human the code fails ?

Thank you for your explanation

  • 写回答

2条回答 默认 最新

  • dongtiao2105 2017-11-06 16:25
    关注

    Your primary problem is that you're using a pointer to an interface. My answer to this question contains some details about the difference. Suffice it to say, pointers to interfaces are almost always errors.

    If you store a *Man into a Human (NOT a *Human), it'll work just fine because interfaces can store pointers without a problem. In fact, in general, you want to default to storing pointers in interfaces, as values stored in interfaces cannot access pointer methods of the type stored. An interface is simply a bucket that holds a type, and it doesn't matter whether that type is a struct or a pointer to a struct. A pointer to an interface, on the other hand, is NOT an interface, and doesn't have the same implicit fulfillment system that interfaces have. It's much the same as being unable to use a *func() variable as a function. It's a pointer, not a function.

    TL;DR: don't use pointers to interfaces. It's almost never useful, and generally just reflects a misunderstanding of what interfaces are.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据