注:
本文代码来自https://zhuanlan.zhihu.com/p/47616970
解释资料来自https://www.jianshu.com/p/d93656cdce0a
先看这一段代码
package mainimport ("fmt"
)type People interface {
Speak(string) string
}type Stduent struct{
}func (stu *Stduent) Speak(think string) (talk string) {
if think == "eat" {
talk = "You are a good boy"} else {
talk = "hi"}return
}func main() {
var peo People = Stduent{
}think := "eat"fmt.Println(peo.Speak(think))
}
这段代码是编译不通过的,原因如下
我就感觉奇怪,明明Student实现了Speak方法,为什么报错说没有实现呢?
这里就涉及到一个【方法集】的概念了,第一次接触的同学可以去百度学习一下。
其中,Student实现的Speak方法中,"(stu Student)"这个叫做接收器(receiver),如果变量是T,那么接收器类型为(t T)的才算变量T实现的方法;如果变量是T,那么接收器类型为(t T)和(t *T)的都算变量T实现的方法。写成表格的形式如下:
变量类型 | 方法接收器类型 |
---|---|
T | (t T) |
*T | (t T)+(t *T) |
看回我们的代码,peo声明的是Student{},也就是普通的T,所以只能接收(t T)的方法,而我们的Speak是(stu *Student)的,所以就有了上面的错误。
修改很简单,把peo声明为指针类型,或者把接收器的指针标识去掉
方法1:修改变量类型
package mainimport ("fmt"
)type People interface {
Speak(string) string
}type Stduent struct{
}func (stu *Stduent) Speak(think string) (talk string) {
if think == "eat" {
talk = "You are a good boy"} else {
talk = "hi"}return
}func main() {
var peo People = &Stduent{
}think := "eat"fmt.Println(peo.Speak(think))
}
方法2:修改接收器类型
package mainimport ("fmt"
)type People interface {
Speak(string) string
}type Stduent struct{
}func (stu Stduent) Speak(think string) (talk string) {
if think == "eat" {
talk = "You are a good boy"} else {
talk = "hi"}return
}func main() {
var peo People = Stduent{
}think := "eat"fmt.Println(peo.Speak(think))
}
方法3:
改成 peo := Stduent{} 即可