码迷,mamicode.com
首页 > 其他好文 > 详细

(三十四)golang--接口

时间:2019-11-23 18:02:47      阅读:63      评论:0      收藏:0      [点我收藏+]

标签: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)
}

(三十四)golang--接口

标签:stop   computer   func   hone   put   lan   耦合   现在   star   

原文地址:https://www.cnblogs.com/xiximayou/p/11918740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!