Go 接口

image

当前结构体方法只要将接口上定义的方法全部实现,通过该结构体实例化后的实例就可以当为接口实例

type person interface {
   walk()
   talk()
   smile()
}

type student struct {
   name string
}

func (s student) talk() {
   fmt.Println("说话", s.name)
}

func (s student) smile() {
   fmt.Println("微笑", s.name)
}

func (s student) walk() {
   fmt.Println("走路", s.name)
}

func main() {
   var tom person = student{"tom"}
   tom.talk()
}
posted @ 2023-03-14 13:08  ForLivetoLearn  阅读(11)  评论(0编辑  收藏  举报