Golang_interface接口
接口的定义比较抽象,但是如果想象实际的物体就比较好理解了
比如USb接口,和很多手机都可以相接。
可以说接口形成了一种规范。
实例1
package main import "fmt" //声明一个接口 type Usb interface{ //声明两个方法 Start() Stop() } type Phone struct{ } type Camoro struct{ } func (p Phone) Start(){ fmt.Println("手机工作...") } func (p Phone) Stop(){ fmt.Println("手机停止工作...") } func (c Camoro)Start(){ fmt.Println("相机工作...") } func (c Camoro)Stop(){ fmt.Println("相机停止工作...") } type Computer struct{ } //编写Work方法接受Usb接口类型的变量 //只要是实现了USB接口,所谓实现USB接口,就是指实现了USB接口声明的所有方法 func (co Computer) work(usb Usb){ usb.Start() usb.Stop() } func main(){ computer := Computer{} phone := Phone{} camoro := Camoro{} computer.work(phone) computer.work(camoro) }
上面的实例首先定义了两个结构体和USb接口,通过结构体和Usb接口的联系输出。
比较抽象
实例2
type Usb interface{ Say() } type Student struct{ } func (stu *Student)Say(){ fmt.Println("Say...") } func main(){ var stu Student var u Usb = &stu u.Say() fmt.Println(u) }
上面实例通过指针类型传值
在Go的底层指针都有优化
实例3
package main import "fmt" type Monkey struct{ name string } func (m *Monkey)climbing(){ fmt.Println(m.name,"会爬树") } type littleMonkey struct{ Monkey } type bird interface{ flying() } func (b *Monkey)flying(){ fmt.Println(b.name,"会飞") } type fish interface{ swimming() } func (f *Monkey)swimming(){ fmt.Println(f.name,"会游泳") } func main(){ monkey := littleMonkey{ Monkey{ name : "孙悟空", }, } monkey.climbing() monkey.swimming() monkey.flying() }
示例4
/** * @Author QY * @Email 80013593@qq.com * @Description //聚合支付,策略模式 * @Date 9:32 AM 8/14/23 * @Param * @return **/ func test() { payRequest := request.PayRequest{} payment := NewPayment(&WxPay{ &wxpay.PaymentConf{ AppId: "wx1323234343434", NotifyURL: "weixin.notifyURL.com", }, }) payment.Pay(payRequest) fmt.Println() apay := NewPayment(&AliPay{ &alipay.PaymentConf{ AppId: "al1323234343434", NotifyURL: "ali.notifyURL.com", }, }) apay.Pay(payRequest) } type Context struct { strategy PaymentStrategy } type PaymentStrategy interface { //订单支付 OrderPayment(payRequest request.PayRequest) //订单退款 OrderRefund(payRequest request.PayRequest) //根据订单查看支付状态 OrderQueryByOrderNo(payRequest request.PayRequest) } func NewPayment(strategy PaymentStrategy) *Context { return &Context{ strategy: strategy, } } func (p *Context) Pay(payRequest request.PayRequest) { p.strategy.OrderPayment(payRequest) } type WxPay struct { *wxpay.PaymentConf } func (w *WxPay) OrderPayment(payRequest request.PayRequest) { pay:=wxpay.WxPay{} pay.OrderPayment(payRequest) } func (w *WxPay) OrderRefund(payRequest request.PayRequest) { fmt.Printf("Refund %d元 to %s by WxPay", payRequest.OrderAmount, payRequest.OrderNo) } func (w *WxPay) OrderQueryByOrderNo(payRequest request.PayRequest) { fmt.Printf("OrderNo %d元 to %s by WxPay", payRequest.OrderAmount, payRequest.OrderNo) } type AliPay struct { *alipay.PaymentConf } func (a *AliPay) OrderPayment(payRequest request.PayRequest) { fmt.Printf("Pay %d元 to %s by AliPay", payRequest.OrderAmount, payRequest.OrderNo) } func (a *AliPay) OrderRefund(payRequest request.PayRequest) { fmt.Printf("Refund %d元 to %s by AliPay", payRequest.OrderAmount, payRequest.OrderNo) } func (a *AliPay) OrderQueryByOrderNo(payRequest request.PayRequest) { fmt.Printf("OrderNo %d元 to %s by AliPay", payRequest.OrderAmount, payRequest.OrderNo) }
由实例可知,接口是继承的补充。
接口可以说是完成了继承不可以做的事情
2 继承和接口区别
继承的价值在于代码的复用性,和可维护性
接口的价值在于设计
接口与继承更加灵活。
继承是实现is a的关系
接口实现的like a的关系
接口在一定程度上实现了代码的解耦