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)
}