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()
}

從編輯器來看,接口有實現類
在這裏插入圖片描述

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