标签:stop computer func hone put lan 耦合 现在 star
golang的多态特性主要体现在接口上;
主要优势:高内服低耦合;
package main import ( "fmt" ) type usb interface { start() stop() } type phone struct { } func (p phone) start() { fmt.Println("手机开始工作") } func (p phone) stop() { fmt.Println("手机停止工作") } type camera struct { } func (c camera) start() { fmt.Println("相机开始工作") } func (c camera) stop() { fmt.Println("相机停止工作") } type computer struct { } func (co computer) working(usb usb) { usb.start() usb.stop() } func main() { computer := computer{} phone := phone{} camera := camera{} computer.working(phone) computer.working(camera) }
标签:stop computer func hone put lan 耦合 现在 star
原文地址:https://www.cnblogs.com/xiximayou/p/11918740.html