golang interface实现中遇到的问题

package main

import (
       "fmt"
)

type Human struct {
       name string
       age int
       phone string
}

type Student struct {
       Human
       school string
       loan float32
}

type Employee struct {
       Human
       company string
       money float32
}

func (h Human) SayHi()  {
       fmt.Printf("Hi,I am %s you can call me on %s\n",h.name,h.phone)
}

func (h Human) sing(lyrics string)  {
       fmt.Println("la la la la la la la la la la la la ...",lyrics)
}


func (e Employee) SayHi()  {
       fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
              e.company, e.phone) //Yes you can split into 2 lines here.
}

type Men interface {
       SayHi()
       sing(lyrics string)
}

func main()  {
       mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}
       paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}
       sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}
       Tom := Employee{Human{"Sam", 36, "444-222-XXX"}, "Things Ltd.", 5000}

       //定义Men类型的变量i
       var i Men

       //i能存储Student
       i = mike
       fmt.Println("this is mike, astudent")
       i.SayHi()
       i.sing("November rain")

       //i 也能存储Employee
       i = Tom
       fmt.Println("this is tom, an Employee:")
       i.SayHi()
       i.sing("Born to be wild")

       //定义了slice Men
       fmt.Println("Let's use a slice of Men and see what happens")
       x := make([]Men,3)
       //这三个都是不同类型的元素,但是他们实现了interface同一个接口
       x[0],x[1],x[2] = paul,sam,mike

       for _,value := range x{
              value.SayHi()
       }

}

以上代码可以运行,但是把mathoed中的struct换成指针为什么就不能运行了,如下所示

package main

import (
       "fmt"
)

type Human struct {
       name string
       age int
       phone string
}

type Student struct {
       Human
       school string
       loan float32
}

type Employee struct {
       Human
       company string
       money float32
}

func (h *Human) SayHi()  {
       fmt.Printf("Hi,I am %s you can call me on %s\n",h.name,h.phone)
}

func (h *Human) sing(lyrics string)  {
       fmt.Println("la la la la la la la la la la la la ...",lyrics)
}


func (e *Employee) SayHi()  {
       fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
              e.company, e.phone) //Yes you can split into 2 lines here.
}

type Men interface {
       SayHi()
       sing(lyrics string)
}

func main()  {
       mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}
       paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}
       sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}
       Tom := Employee{Human{"Sam", 36, "444-222-XXX"}, "Things Ltd.", 5000}

       //定义Men类型的变量i
       var i Men

       //i能存储Student
       i = mike
       fmt.Println("this is mike, astudent")
       i.SayHi()
       i.sing("November rain")

       //i 也能存储Employee
       i = Tom
       fmt.Println("this is tom, an Employee:")
       i.SayHi()
       i.sing("Born to be wild")

       //定义了slice Men
       fmt.Println("Let's use a slice of Men and see what happens")
       x := make([]Men,3)
       //这三个都是不同类型的元素,但是他们实现了interface同一个接口
       x[0],x[1],x[2] = paul,sam,mike

       for _,value := range x{
              value.SayHi()
       }
}
报错是接口不能接受此种类型了 如下:

"C:\Program Files (x86)\JetBrains\Gogland 163.10154.18\bin\runnerw.exe" C:/Go\bin\go.exe run C:/Users/Administrator/GoglandProjects/untitled/src/test.go
# command-line-arguments
src\test.go:54: cannot use mike (type Student) as type Men in assignment:
Student does not implement Men (SayHi method has pointer receiver)
src\test.go:60: cannot use Tom (type Employee) as type Men in assignment:
Employee does not implement Men (SayHi method has pointer receiver)
src\test.go:69: cannot use paul (type Student) as type Men in assignment:
Student does not implement Men (SayHi method has pointer receiver)
src\test.go:69: cannot use sam (type Employee) as type Men in assignment:
Employee does not implement Men (SayHi method has pointer receiver)
src\test.go:69: cannot use mike (type Student) as type Men in assignment:
Student does not implement Men (SayHi method has pointer receiver)


Process finished with exit code 2


对此深深不解,以作记录。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值