Go学习交流

GO学习笔记09

2018-06-26  本文已影响21人  Q大疯zi

接口

1.接口定义

    package main
    
    import "fmt"
    
    //定义接口类型
    type Humaner interface {
        sayHi()
    }
    
    type Student struct {
        name string
        id   int
    }
    
    func (stu *Student) sayHi() {
        fmt.Printf("Student[%s,%d] sayHi\n", stu.name, stu.id)
    }
    
    type Teacher struct {
        addr  string
        group string
    }
    
    func (teacher *Teacher) sayHi() {
        fmt.Printf("Teacher[%s,%s] sayHi\n", teacher.addr, teacher.group)
    }
    
    type Mystr string
    
    func (str *Mystr) sayHi() {
        fmt.Printf("Mystr[%s] sayHi\n", *str)
    }
    
    func main() {
        var human Humaner
        s := &Student{"tony", 77}
        human = s
        human.sayHi()
    
        t := &Teacher{"tom", "go"}
        human = t
        human.sayHi()
    
        var str Mystr = "Hello Tony"
        human = &str
        human.sayHi()
    }
    
    
    Student[tony,77] sayHi
    Teacher[tom,go] sayHi
    Mystr[Hello Tony] sayHi

2.多态

    package main
    
    import "fmt"
    
    //定义接口类型
    type Humaner interface {
        sayHi()
    }
    
    type Student struct {
        name string
        id   int
    }
    
    func (stu *Student) sayHi() {
        fmt.Printf("Student[%s,%d] sayHi\n", stu.name, stu.id)
    }
    
    type Teacher struct {
        addr  string
        group string
    }
    
    func (teacher *Teacher) sayHi() {
        fmt.Printf("Teacher[%s,%s] sayHi\n", teacher.addr, teacher.group)
    }
    
    type Mystr string
    
    func (str *Mystr) sayHi() {
        fmt.Printf("Mystr[%s] sayHi\n", *str)
    }
    
    //定义一个普通函数,函数的参数为接口类型
    //只有一个函数,可以有不同的表现,多态
    func WhoSayHi(humaner Humaner) {
        humaner.sayHi()
    }
    
    func main() {
        s := &Student{"tony", 77}
        t := &Teacher{"tom", "go"}
        var str Mystr = "Hello Tony"
    
        //调用同一个函数,不同的表现
        WhoSayHi(s)
        WhoSayHi(t)
        WhoSayHi(&str)
    
        //创建一个切片
        x := make([]Humaner, 3)
        x[0] = s
        x[1] = t
        x[2] = &str
    
        fmt.Println("------------------------")
        //第一个返回下标,第二个返回下标所对应的值
        for _, i := range x {
            i.sayHi()
        }
    
    }
    
    Student[tony,77] sayHi
    Teacher[tom,go] sayHi
    Mystr[Hello Tony] sayHi
    ------------------------
    Student[tony,77] sayHi
    Teacher[tom,go] sayHi
    Mystr[Hello Tony] sayHi

3.接口的继承

 package main
    
    import "fmt"
    
    //定义接口类型
    type Humaner interface {        //子集
        sayHi()
    }
    
    type Personer interface {       //超集
        Humaner //匿名字段,继承sayHi()
        sing(lrc string)
    }
    
    type Student struct {
        name string
        id   int
    }
    
    //Student实现了sayHi
    func (stu *Student) sayHi() {
        fmt.Printf("Student[%s,%d] sayHi\n", stu.name, stu.id)
    }
    
    func (tmp *Student) sing(lrc string) {
        fmt.Println("Student在唱着:", lrc)
    }
    
    func main() {
        var i Personer
        s := &Student{"Tony", 66}
        i = s
    
        i.sayHi()
        i.sing("好汉歌")
    }
    
    
    Student[Tony,66] sayHi
    Student在唱着: 好汉歌

4.接口转换

    package main
    
    import "fmt"
    
    //定义接口类型
    type Humaner interface {
        sayHi()
    }
    
    type Personer interface {
        Humaner //匿名字段,继承sayHi()
        sing(lrc string)
    }
    
    type Student struct {
        name string
        id   int
    }
    
    //Student实现了sayHi
    func (stu *Student) sayHi() {
        fmt.Printf("Student[%s,%d] sayHi\n", stu.name, stu.id)
    }
    
    func (tmp *Student) sing(lrc string) {
        fmt.Println("Student在唱着:", lrc)
    }
    
    func main() {
        //超集可以转换成子集,反过来不可以
        var iPro Personer
        iPro = &Student{"mike", 777}
    
        var i Humaner
    
        //iPro = i //err
        i = iPro
        i.sayHi()
    
    }
    
    
    Student[mike,777] sayHi

5.空接口

    package main
    
    import "fmt"
    
    //定义这样的方法可以
    func xxx(arg ...interface{}) {
    
    }
    
    func main() {
        //空接口万能类型,保存任意类型的值
        var i interface{} = 1
        fmt.Println("i = ", i)
    
        i = "abc"
        fmt.Println("i = ", i)
    
    }
    
    
    i =  1
    i =  abc

6.类型判断之断言

    package main
    
    import "fmt"
    
    type Cat struct {
        name string
        id   int
    }
    
    func main() {
        i := make([]interface{}, 3)
        i[0] = 1
        i[1] = "hello go"
        i[2] = Cat{"tom", 3}
    
        for index, data := range i {
            if value, ok := data.(int); ok == true {
                fmt.Printf("x[%d] 类型为int,内容为%d\n", index, value)
            } else if value, ok := data.(string); ok == true {
                fmt.Printf("x[%d] 类型为string,内容为%s\n", index, value)
            } else if value, ok := data.(Cat); ok == true {
                fmt.Printf("x[%d] 类型为Cat,name = %s,id =%d\n ", index, value.name, value.id)
            }
        }
    
    }
    
    
    x[0] 类型为int,内容为1
    x[1] 类型为string,内容为hello go
    x[2] 类型为Cat,name = tom,id =3

7.类型判断之switch

    package main
    
    import "fmt"
    
    type Cat struct {
        name string
        id   int
    }
    
    func main() {
        i := make([]interface{}, 3)
        i[0] = 1
        i[1] = "hello go"
        i[2] = Cat{"tom", 3}
    
        for index, data := range i {
            switch value:=data.(type) {
            case int:
                fmt.Printf("x[%d] 类型为int,内容为%d\n", index, value)
            case string:
                fmt.Printf("x[%d] 类型为string,内容为%s\n", index, value)
            case Cat:
                fmt.Printf("x[%d] 类型为Cat,name = %s,id =%d\n ", index, value.name, value.id)
            }
        }
    }
    
    
    x[0] 类型为int,内容为1
    x[1] 类型为string,内容为hello go
    x[2] 类型为Cat,name = tom,id =3
上一篇下一篇

猜你喜欢

热点阅读