关于go:Go一文玩转接口

0次阅读

共计 2827 个字符,预计需要花费 8 分钟才能阅读完成。

  1. 接口的根本分析

    package main
    
    import "fmt"
    
    type Test interface {show()
    }
    
    type myString string
    
    func (mys myString) show() {fmt.Println(mys)
    }
    
    func main() {
     var a myString = "tantianran"
     a.show()}

    输入:

    tantianran

    定义接口:上述代码中,定义了接口 Test,该接口规定了有 show() 办法,而且,Test 接口只有 1 个办法。

实现接口:接下来就是实现接口,在 go 中,任意类型只有实现了接口的所有办法(这里的 Test 接口只有 1 个办法),那么就实现了该接口(而且是隐式实现),刚提到任意类型,于是这里定义了一个类型为 string 的自定义类型 myString,且由该自定义类型 myString 实现了 show() 办法,刚提到只有实现了接口的所有办法就实现了该接口,也就是说自定义的 myString 类型曾经实现了 Test 接口。

应用接口:定义了类型为 myString 的变量 a,且给它赋值了 ”tantianran”,这时候,它就领有了 show 办法。所以说,一个变量不论它是什么类型,只有实现了 Test 接口,就能调用它的 show 办法。

  1. 在上一个栗子的根底上,持续看看这个小栗子

    刚提到任意类型只有实现了 Test 接口,就能调用它的 show 办法,来验证一下

    package main
    
    import "fmt"
    
    type Test interface {show()
    }
    
    type myInts int16
    
    func (myi myInts) show() {fmt.Println(myi)
    }
    
    func main() {
     var a myInts = 200
     a.show()}

    输入:

    200

    果然,自定义了一个 int16 类型的自定义类型 myInts,它也用了 show 办法。

  2. 持续上一个例子,看看上面代码

    定义个构造体类型的 User 试试

    package main
    
    import "fmt"
    
    type Test interface {show()
    }
    
    type User struct {
     name string
     age  int
    }
    
    func (u User) show() {fmt.Println(u)
    }
    
    func main() {u := User{"tantianran", 18}
     u.show()}

    输入:

    {tantianran 18}
  3. 来一个贴近生活的小栗子

    package main
    
    import "fmt"
    
    // 领取接口
    type Payment interface {Payment() float64
    }
    
    // 银行卡
    type Bankcard struct {
     CardId     int64
     Moneytotal float64
    }
    
    // 结算
    func settleAccount(b *Bankcard, p Payment) {fmt.Println("正在产生扣款...")
     amount := p.Payment()
     res := b.Moneytotal - amount
     b.Moneytotal = res
    }
    
    // 微信
    type Weixin struct {
     AccountID string
     Type      string
    }
    
    func (w Weixin) Payment() float64 {return 65.78}
    
    // 支付宝
    type Zhifubao struct {
     AccountID string
     Type      string
    }
    
    func (z Zhifubao) Payment() float64 {return 82.12}
    
    type PingDuoduo struct {
     Commodity string  // 商品
     Price     float64 // 价格
    }
    
    func main() {card := Bankcard{CardId: 2349342594759947742, Moneytotal: 1200.89}
     fmt.Println(card.Moneytotal)
     wx := Weixin{AccountID: "ttr", Type: "微信"}
     zfb := Zhifubao{AccountID: "ttr1", Type: "支付宝"}
     settleAccount(&card, wx)
     settleAccount(&card, zfb)
    
     fmt.Println(card.Moneytotal)
    
    }
  4. 通过接口实现构造体排序

    自定义的构造体只有实现了 Len、Less、Swap 办法,就能够交给规范库中的 sort.Sort 进行排序。

    package main
    
    import (
     "fmt"
     "math/rand"
     "sort"
    )
    
    type Hero struct {
     Name string
     Age  int
    }
    
    type HeroSlice []Hero
    
    func (hs HeroSlice) Len() int {return len(hs)
    }
    
    func (hs HeroSlice) Less(i, j int) bool {return hs[i].Age < hs[j].Age
    }
    
    func (hs HeroSlice) Swap(i, j int) {temp := hs[i]
     hs[i] = hs[j]
     hs[j] = temp
    }
    
    func main() {
     var heroes HeroSlice
     for i := 0; i < 10; i++ {
         hero := Hero{Name: fmt.Sprintf("host-%d", rand.Intn(100)),
             Age:  rand.Intn(100),
         }
         heroes = append(heroes, hero)
     }
    
     sort.Sort(heroes)
     for _, v := range heroes {fmt.Println(v)
     }
    }
  5. 函数形参,类型是空接口时能够接管任意类型

    package main
    
    import "fmt"
    
    func test(t interface{}) {fmt.Println(t)
    }
    
    type User struct {name string}
    
    func main() {
     a := 1
     b := "hello"
     c := map[string]int{"age": 18,}
     u := User{name: "ttr"}
    
     z := []interface{}{"hello", 100, 89.78, c, a, b}
    
     test(a)
     test(b)
     test(c)
     test(u)
    
     test(z)
    }
  6. 变量的类型为空接口时,也能够接管任意类型的值

    package main
    
    import "fmt"
    
    type myinterface interface{}
    
    func main() {
     var a myinterface = 10
     fmt.Println(a)
     var b myinterface = 89.78
     fmt.Println(b)
    
     numarr := []int{1, 2, 3, 4, 5}
     var c myinterface = numarr
     fmt.Println(c)
    
     user := map[string]string{
         "name": "ttr",
         "addr": "gz",
     }
     var d myinterface = user
     fmt.Println(d)
    }

本文转载于(喜爱的盆友关注咱们):https://mp.weixin.qq.com/s/4M…

正文完
 0