美文网首页程序员
GO——学习笔记(八)

GO——学习笔记(八)

作者: 就天边 | 来源:发表于2016-11-25 18:08 被阅读324次

上篇:GO——学习笔记(七)

下篇:GO——学习笔记(九)

参考:

https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/02.6.md

示例代码——go_7

https://github.com/jiutianbian/golang-learning/blob/master/go_7/main.go

interface

interface基本定义

各个编程语言关于接口的定义:接口相当于是一份契约,它规定了一个对象所能提供的一组操作。

golang中的interface也是此作用:interface是就是一组method签名的组合,我们可以通过interface来定义对象的一组行为。在golang中,假如一个对象实现了interface中的所有method,那么我们就说此对象实现了此interface,通过interface我们可以让面向对象和内容组织实现非常的方便。

上面说了这么多概念,我们来看下,具体的interface的声明,实现,以及作用

interface的声明与对象实现interface

例如现在定义了一个interface叫做function,它其中有两个method,run()和stop(),那么现在再定义两个不同的对象,car和bike,他们都实现了run()和stop()方法,我们就说car和bike实现了这个叫做function的interface,代码如下

//声明一个叫function的接口
type function interface {
    run()
    stop()
}

//申明三个对象 car 和 bike,hunman
type car struct {
    brand string
    price string
    speed string
    size  string
}

type bike struct {
    brand string
    price string
    speed string
}

type human struct {
    name string
    age  string
}

//car 和 bike 都实现了run和stop方法,我们就说car和bike实现了function这个interface,而 human 没有实现run方法,就没有实现function这个interface
func (c car) run() {
    fmt.Println("我是汽车:", c.brand, c.price, c.speed, "我跑起来了")
}

func (c car) stop() {
    fmt.Println("我是汽车:", c.brand, c.price, c.speed, "我停下来了")
}

func (b bike) run() {
    fmt.Println("我是自行车:", b.brand, b.price, b.speed, "我跑起来了")
}

func (b bike) stop() {
    fmt.Println("我是自行车:", b.brand, b.price, b.speed, "我停下来了")
}

func (h human) stop() {
    fmt.Println("我是:", h.name, h.age, "我停下来了")
}

interface使用

简单使用

如果定义个interface变量,那这个变量就能够持实现了这个interface的任意类型的对象。

我们通过代码来了解,上面定义了的function这个interface,和3个对象car,bike,human,其中car,bike实现了function这个interface,而human没有,我们现在定义一个类型为function的fc变量,fc可以持有实现了function这个interface方法的对象car和bike的变量,benchi和fenghaung,不能持有human变量yaoming,如果持有会报错

func main() {
    //定义一个类型为function的变量fc
    var fc function

    var benchi car
    var fenghaung bike
    //  var yaoming human

    benchi = car{"奔驰", "60万", "140km/h", "很大"}
    fenghaung = bike{"凤凰", "1k", "20km/h"}
    //  yaoming = human{"姚明", "40"}

    //fc能够持有实现了function这个interface方法的对象car和bike的变量,benchi和fenghaung,不能持有human,如果持有会报错
    fc = benchi
    fc.run()
    fc.stop()

    fc = fenghaung
    fc.run()
    fc.stop()

    //  fc = yaoming
    //  yaoming.stop()
}
空interface

如果我们定义了一个空的interface,由于interface里面不包含任何的method,所以任意类型都实现了此interface,所以空的interface就能够持有任意类型的变量值,代码如下

type nullInterface interface{}

func testNullInterface() {
    var jiaobaba nullInterface

    var age int
    var name string
    var yaoming human

    name = "易建联"
    age = 15
    yaoming = human{"姚明", "40"}

    //jiaobaba这个空的interface变量能够持有任意类型
    jiaobaba = name
    fmt.Println(jiaobaba)

    jiaobaba = age
    fmt.Println(jiaobaba)

    jiaobaba = yaoming
    fmt.Println(jiaobaba)

}
interface函数参数

既然interface变量能够持有实现了此interface方法的的变量,那么我就可以定义函数参数为interface来接受不同类型的参数,代码如下:

// function这个interface作为参数,可以接受实现了他的对象car和bike的变量
func showYourFunction(fc function) {
    fc.run()
    fc.stop()
}

func testParamInterface() {
    benchi := car{"奔驰", "60万", "140km/h", "很大"}
    fenghaung := bike{"凤凰", "1k", "20km/h"}

    showYourFunction(benchi)
    showYourFunction(fenghaung)
}
如何判断interface变量存储的具体类型

Go语言里面有一个语法,Comma-ok断言,可以直接判断是否是该类型的变量: value, ok = element.(T),这里value就是变量的值,ok是一个bool类型,element是interface变量,T是断言的类型,代码如下

type nullInterface interface{}

func showInterfaceType(nl nullInterface) {
    value, ok := nl.(int)

    fmt.Println("value:", value, "ok", ok)
    //打印 value: 0 ok: false  value: 1 ok: true
}

func testCommaInterface() {
    showInterfaceType("1")
    showInterfaceType(1)
}

相关文章

网友评论

    本文标题:GO——学习笔记(八)

    本文链接:https://www.haomeiwen.com/subject/edqwpttx.html