
























type 接口名 interface { method1(参数列表) 返回值列表 method2(参数列表) 返回值列表 }
package main import ( "fmt" ) type Usb interface{ Connect() DisConnect() } type Phone struct{ } /* * Phone实现了Usb 接口(是指实现了Usb接口的所有方法) */ func(p Phone) Connect(){ fmt.Println("手机连接...") } func(p Phone) DisConnect(){ fmt.Println("手机断开连接...") } type Camera struct{ } func(c Camera) Connect(){ fmt.Println("相机连接...") } func(c Camera) DisConnect(){ fmt.Println("相机断开连接...") } type Computer struct{ } func(c Computer) Working(u Usb){ u.Connect() u.DisConnect() } func main(){ phone := Phone{} camera := Camera{} computer := Computer{} computer.Working(phone) computer.Working(camera) }
运行结果如下:

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。