新人刚学,在接口这里一直有点困惑,接口定义是实现了该接口的方法就是实现了该接口,我直接定一个函数直接去实现不更好?
```
package main
import "fmt"
type AInterface interface {
InterFaceSay(name string)
}
type Student struct {
Name string
}
type Teacher struct {
Name string
}
func (s Student) InterFaceSay(name string) {
fmt.Printf("%s say\n",name)
}
func (t Teacher) InterFaceSay(name string) {
fmt.Printf("%s say\n",name)
}
func Say(s string) {
fmt.Printf("%s say\n",s)
}
func main() {
s1 := Student{
Name:"stu1",
}
var a AInterface = s1
a.InterFaceSay(s1.Name)
t1 := Teacher{
Name:"tea1",
}
var b AInterface = t1
b.InterFaceSay(t1.Name)
Say(s1.Name)
Say(t1.Name)
}
```
有疑问加站长微信联系(非本文作者)