GO學習筆記09

接口

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.多態(tài)

    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)
    }
    
    //定義一個普通函數(shù),函數(shù)的參數(shù)為接口類型
    //只有一個函數(shù),可以有不同的表現(xiàn),多態(tài)
    func WhoSayHi(humaner Humaner) {
        humaner.sayHi()
    }
    
    func main() {
        s := &Student{"tony", 77}
        t := &Teacher{"tom", "go"}
        var str Mystr = "Hello Tony"
    
        //調(diào)用同一個函數(shù),不同的表現(xiàn)
        WhoSayHi(s)
        WhoSayHi(t)
        WhoSayHi(&str)
    
        //創(chuàng)建一個切片
        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實現(xiàn)了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.接口轉(zhuǎn)換

    package main
    
    import "fmt"
    
    //定義接口類型
    type Humaner interface {
        sayHi()
    }
    
    type Personer interface {
        Humaner //匿名字段,繼承sayHi()
        sing(lrc string)
    }
    
    type Student struct {
        name string
        id   int
    }
    
    //Student實現(xiàn)了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() {
        //超集可以轉(zhuǎn)換成子集,反過來不可以
        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,內(nèi)容為%d\n", index, value)
            } else if value, ok := data.(string); ok == true {
                fmt.Printf("x[%d] 類型為string,內(nèi)容為%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,內(nèi)容為1
    x[1] 類型為string,內(nèi)容為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,內(nèi)容為%d\n", index, value)
            case string:
                fmt.Printf("x[%d] 類型為string,內(nèi)容為%s\n", index, value)
            case Cat:
                fmt.Printf("x[%d] 類型為Cat,name = %s,id =%d\n ", index, value.name, value.id)
            }
        }
    }
    
    
    x[0] 類型為int,內(nèi)容為1
    x[1] 類型為string,內(nèi)容為hello go
    x[2] 類型為Cat,name = tom,id =3
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 三重:代碼、底層內(nèi)存、源碼 第一階段:開發(fā)常用JavaSE基礎、IDE、Maven、Gradle、SVN、Git、...
    guodd369閱讀 17,525評論 1 44
  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學習記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡書還為我保存起的...
    Jenaral閱讀 3,146評論 2 9
  • 遷移學習是將一個已經(jīng)訓練好的多層神經(jīng)網(wǎng)絡的后幾層拿掉,替換成另一個任務的輸出層。已訓練完的神經(jīng)網(wǎng)絡提供前面幾層的權...
    劉大力_閱讀 635評論 0 0
  • 地鐵站到宿舍的距離 2.2公里 騎車13分鐘 步行我沒試過 坐公交45分鐘
    公園閱讀 291評論 0 2
  • 第四十六章 他要世俗化自已 會議結(jié)束以后,村組干部留下來,繼續(xù)討論,李書記對大小隊干部說:大家這次一定要努力...
    劉歸真閱讀 328評論 2 10

友情鏈接更多精彩內(nèi)容