zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Golang 接口

2023-09-14 08:56:53 时间

  结构体通过实现接口中定义的方法来实现接口。

简单入门

package main
import "fmt"
type Phone interface {
	call(name string)
}
type Person struct {
	Name string
}

//方法
func (p *Person) Test2(newName string) string {
	p.Name = newName
	return p.Name
}

//接口 func (p Person) call(name string) string { return p.Name + "打电话给" + name } func main() { p1 := Person{"abc"} str := p1.call("xyz") fmt.Println(str) //abc打电话给xyz }

  

结构体赋值接口

package main
import "fmt"
type Person struct {
	Name string
}
type notifier interface {
	notice()
}
//实现接口
func (p Person) notice() {
	fmt.Println(p.Name, "noticing")
}
func main() {
	var p notifier    //注意类型
	p = Person{"abc"}
	p.notice() //abc noticing
}

  如果一个struct实现了interface中的所有方法,那么可以将一个struct的变量赋值给一个interface,只实现一部分方法,那么就不能赋值操作。

  这个例子中,如果notifier中还有一个demo()方法,但是person没有实现这个demo(),那么在主函数中执行赋值就会出错,提示没有实现notifier的所有接口。

 

接口组合

package main
import "fmt"
type Person struct {
	Name string
}
type notifier interface {
	notice()
}
type informer interface {
	inform()
}
//type tell interface {
//	notifier
//	informer
//}
//或者
type tell interface {
	notice()
	inform()
}
func (p Person) notice() {
	fmt.Println(p.Name, "noticing")
}
func (p Person) inform() {
	fmt.Println(p.Name, "informing")
}
func main() {
	var p tell	//注意p是组合接口的类型
	p = Person{"abc"}
	p.notice() //abc noticing
}

  只要两个接口的拥有相同的方法列表,方法的次序可以不同,那么这两个接口是等价的,可以相互赋值。

  所以说上面tell组合接口的两种形式是一样的。

 

多态

package main
import "fmt"
type Teacher struct {
	Name string
}
type Student struct {
	Name string
}
type notifier interface {
	notice()
}
func (t Teacher) notice() {
	fmt.Println(t.Name, "noticing")
}
func (s Student) notice() {
	fmt.Println(s.Name, "informing")
}
//sendMsg接收一个实现了notifier接口的struct
func sendMsg(n notifier) {
	n.notice()
}
func main() {
	t := Teacher{"abx"}
	s := Student{"xyz"}
	sendMsg(t)		//abx noticing
	sendMsg(s)		//xyz informing
}

  其实多态最主要的就是接受的那个参数类型是一个接口类型,并且是多个结构体实现了的接口,结合前面一个示例,那么实现了接口的结构体实例可以赋值给接口类型的变量,从而调用接口中定义的方法。

  这里会涉及一个空接口,因为空接口内部什么都没有,一个方法都没有,那么皆可以认为所有的结构体都实现了这个空接口。那么所有结构体都可以赋值给这个空接口的实例。

 

接口查询