1.go里面接口必须使用interface 这个案例也体现了多台特性
package main import "fmt" type Usb interface { work() finish() } type Phone struct { } func ( phone Phone) work(){ fmt.Println("phone is working"); } func (phone Phone) finish(){ fmt.Println("phone is finish"); } type Camera struct { } func(ca Camera) work(){ fmt.Println("camera is workding"); } func (ca Camera) finish(){ fmt.Println("camera is finish"); } type Computer struct { } func (com Computer) start(usb Usb){ usb.work(); usb.finish(); } func main(){ com:=Computer{}; phone:=Phone{}; com.start(phone); }
2.接口使用细节
package main import "fmt" type Usb interface { say() } type Computer struct { } func (com Computer) say(){ fmt.Println("thi sis sya"); } func main(){ var com Computer;//结构体变量 var usb Usb = com; usb.say(); }