GO 实现 interface

接口实现

go语言中的接口实现是一种隐式实现

import "fmt"

type Say interface {
	sayhello()
	saybye()
}

func (t people) sayhello() {
	fmt.Println("hello")
}

// people这个struct 实现Say接口的 saybye方法 
func (t people) saybye() {
	fmt.Println("bye")
}

type people struct {
	age  int
	name string
}

func main() {
   // people赋值给say
	var say Say = people{21, "aa"}
	say.sayhello()
	say.saybye()
}

从编辑器来看,接口有实现类
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章