Golang的接口入门详解
简单入门
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 }
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did18512