【发布时间】:2019-06-26 15:15:43
【问题描述】:
structDog实现了接口Animal的所有方法,为什么*Dos不能赋值给*Animal?
type Animal interface {
run()
}
type Dog struct {
name string
}
func (d *Dog) run() {
fmt.Println( d.name , " is running")
}
func main(){
var d *Dog
var a *Animal
d = new(Dog)
d.run()
a = d //errors here
}
Go 通知以下错误:
Cannot use 'd' (type *Dog) as type *Animal in assignment
【问题讨论】:
标签: go struct interface polymorphism