欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Golang語言中的interface

 更新時間:2021年01月06日 11:09:58   作者:雨燕  
這篇文章主要介紹了Golang語言中的interface的相關(guān)資料,幫助大家更好的理解和使用golang,感興趣的朋友可以了解下

interface是一組method簽名的組合,interface可以被任意對象實現(xiàn),一個對象也可以實現(xiàn)多個interface。任意類型都實現(xiàn)了空interface(也就是包含0個method的interface),空interface可以存儲任意類型的值。interface定義了一組方法,如果某個對象實現(xiàn)了某個接口的所有方法,則此對象就實現(xiàn)了此接口。

go version go1.12

package main

import (
  "fmt"
)

// 定義struct
type Human struct {
  name string
  age  int
  phone string
}
type Student struct {
  Human // 匿名字段
  school string
  loan  float32
}
type Employee struct {
  Human  // 匿名字段
  company string
  money  float32
}

// Human對象實現(xiàn)SayHi()方法
func (h Human) SayHi() {
  fmt.Printf("Hi, I am %s, you can call me on %s\n", h.name, h.phone)
}

// Human對象實現(xiàn)Sing()方法
func (h Human) Sing(lyrics string) {
  fmt.Println("La la la...", lyrics)
}

// Human對象實現(xiàn)Guzzle()方法
func (h Human) Guzzle(beerStein string) {
  fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
}

// Employee對象重寫SayHi()方法
func (e Employee) SayHi() {
  fmt.Printf("Hi I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone)
}

// Student對象實現(xiàn)BorrowMoney()方法
func (s Student) BorrowMoney(amount float32) {
  s.loan += amount
}

// Employee對象實現(xiàn)SpendSalary()方法
func (e Employee) SpendSalary(amount float32) {
  e.money -= amount
}

// 定義interface,interface是一組method簽名的組合
// interface可以被任意對象實現(xiàn),一個對象也可以實現(xiàn)多個interface
// 任意類型都實現(xiàn)了空interface(也就是包含0個method的interface)
// 空interface可以存儲任意類型的值
// interface Men的3個method被Human,Student,Employee實現(xiàn),也就是這3個對象都實現(xiàn)了interface Men。即:
// interface定義了一組方法,如果某個對象實現(xiàn)了某個接口的所有方法,則此對象就實現(xiàn)了此接口。
type Men interface {
  SayHi()
  Sing(lyrice string)
  Guzzle(beerStein string)
}

// interface YoungChap的BorrowMoney() method只被Student對象實現(xiàn),也就是只有Student實現(xiàn)了YoungChap
type YoungChap interface {
  SayHi()
  Sing(song string)
  BorrowMoney(amount float32)
}

// interface ElderlyGent的SpendSalary() method只被Employee對象實現(xiàn),也就是只有Employee實現(xiàn)了ElderlyGent
type ElderlyGent interface {
  SayHi()
  Sing(song string)
  SpendSalary(amount float32)
}

func main() {
  // 定義Student類型的變量
  lucy := Student{Human{"lucy", 19, "10086"}, "tsinghua", 100.00}
  lily := Student{Human{"lily", 19, "10086"}, "tsinghua", 100.00}
  liming := Student{Human{"liming", 19, "10086"}, "tsinghua", 100.00}
  // 定義Employee類型的變量
  tom := Employee{Human{"tom", 29, "10000"}, "Google", 200.00}
  // 定義Men類型的變量i
  var i Men
  // i存儲Student
  i = lucy
  fmt.Println("This is lucy, a student:")
  i.SayHi()
  i.Sing("Happy Birthday")
  i.Guzzle("Ha ha ha...")

  // i存儲Employee
  i = tom
  fmt.Println("This is tom, an Employee:")
  i.SayHi()

  // 定義slice Men,包含Men類型元素的切片,這個slice可以被賦予實現(xiàn)了Men接口的任意結(jié)構(gòu)的對象
  fmt.Println("Let's use a slice of Men and see what happens:")
  x := make([]Men, 3)
  // 三個不同類型(不同Method)的元素,實現(xiàn)了同一個interface(Men)
  x[0], x[1], x[2] = lucy, lily, liming
  for _, value := range x {
    value.SayHi()
  }
}

函數(shù)參數(shù)

interface接口還可以作為函數(shù)參數(shù),因為interface的變量可以持有任意實現(xiàn)該interface類型的對象,我們可以通過定義interface參數(shù),讓函數(shù)接受各種類型的參數(shù)。 判斷interface變量存儲的元素的類型,目前常用的有兩種方法:Comma-ok斷言和switch測試。

go version go1.12

/**
 * interface接口作為函數(shù)參數(shù)
 * 判斷interface變量存儲的元素的類型
 */
package main

import (
  "fmt"
  "strconv"
)

// 定義Human對象
type Human struct {
  name string
  age  int 
  phone string
}

// 定義空接口
type Element interface{}

// 定義切片
type List []Element

// 定義Person對象
type Person struct {
  name string
  age int 
}

// 通過定義interface參數(shù),讓函數(shù)接受各種類型的參數(shù)
// 通過這個Method(方法),Human對象實現(xiàn)了fmt.Stringer接口
// Stringer接口是fmt.Println()的參數(shù),最終使得Human對象可以作為fmt.Println的參數(shù)被調(diào)用
func (h Human) String() string {
  return "<" + h.name + " - " + strconv.Itoa(h.age) + " years - phone: " + h.phone + ">" 
}

// 通過定義interface參數(shù),讓函數(shù)接受各種類型的參數(shù)
// 通過這個Method(方法),Person對象實現(xiàn)了fmt.Stringer接口
// Stringer接口是fmt.Println()的參數(shù),最終使得Person對象可以作為fmt.Println的參數(shù)被調(diào)用
func (p Person) String() string {
  return "(name: " + p.name + " - age: " + strconv.Itoa(p.age) + " years)"
}

func main() {
  // interface作為函數(shù)的參數(shù)傳遞
  Lucy := Human{"Lucy", 29, "10086"}
  fmt.Println("This human is:", Lucy)

  list := make(List, 3)
  list[0] = 100
  list[1] = "Hello Golang!"
  list[2] = Person{"Lily", 19}

  // Comma-ok斷言
  for index, element := range list {
    // 判斷變量的類型 格式:value, ok = element(T)
    // value是interface變量的值,ok是bool類型,element是interface的變量,T是斷言的interface變量的類型
    if value, ok := element.(int); ok {
      fmt.Printf("list[%d] is an int and it's value is %d\n", index, value)
    } else if value, ok := element.(string); ok {
      fmt.Printf("list[%d] is a string and it's value is %s\n", index, value)
    } else if value, ok := element.(Person); ok {
      fmt.Printf("list[%d] is a Person and it's value is %s\n", index, value)
    } else {
      fmt.Printf("list[%d] is a different type\n", index)
    }
  }

  // switch
  for index, element := range list {
    // 注意:element.(type)語法不能在switch外的任何邏輯中使用
    switch value := element.(type) {
    case int:
      fmt.Printf("list[%d] is an int, it's value is %d\n", index, value)
    case string:
      fmt.Printf("list[%d] is a string, it's value is %s\n", index, value)
    case Person:
      fmt.Printf("list[%d] is a Person, it's value is %s\n", index, value)
    default:
      fmt.Printf("list[%d] is a differernt type", index)
    }
  }
}

以上就是詳解Golang語言中的interface的詳細(xì)內(nèi)容,更多關(guān)于Golang語言中的interface的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解Go語言如何解析帶注釋的json

    詳解Go語言如何解析帶注釋的json

    標(biāo)準(zhǔn)的json格式是不帶注釋,但是有時候為了方便理解json中各字段的含義,需要支持帶注釋的json,這篇文章主要介紹了Go語言解析帶注釋json的相關(guān)方法,希望對大家有所幫助
    2024-03-03
  • 使用IDEA配置GO語言的開發(fā)環(huán)境備忘錄

    使用IDEA配置GO語言的開發(fā)環(huán)境備忘錄

    最近在配置idea開發(fā)go語言時碰到很多問題,想著很多人都可能會遇到,所以下面這篇文章主要給大家介紹了關(guān)于使用IDEA配置GO語言的開發(fā)環(huán)境,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Golang連接PostgreSQL基本操作的實現(xiàn)

    Golang連接PostgreSQL基本操作的實現(xiàn)

    PostgreSQL是常見的免費(fèi)的大型關(guān)系型數(shù)據(jù)庫,本文主要介紹了Golang連接PostgreSQL基本操作的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • golang雙鏈表的實現(xiàn)代碼示例

    golang雙鏈表的實現(xiàn)代碼示例

    這篇文章主要介紹了golang雙鏈表的實現(xiàn)代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解Go?語言如何通過測試保證質(zhì)量

    詳解Go?語言如何通過測試保證質(zhì)量

    這篇文章主要為大家介紹了Go?語言如何通過測試保證質(zhì)量詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Go流程控制代碼詳解

    Go流程控制代碼詳解

    這篇文章主要詳細(xì)介紹了Go流程控制,文章通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2023-04-04
  • 關(guān)于Gin框架中的Cookie和Session的使用方法

    關(guān)于Gin框架中的Cookie和Session的使用方法

    為了實現(xiàn)跨請求的數(shù)據(jù)共享,我們可以使用Cookie和Session,本文將結(jié)合實際案例,詳細(xì)介紹在Go語言的Gin框架中如何使用Cookie和Session,并通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • 深入了解Golang中Slice切片的使用

    深入了解Golang中Slice切片的使用

    本文主要為大家詳細(xì)介紹了Golang中Slice切片的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-02-02
  • 使用Go?goroutine實現(xiàn)并發(fā)的Clock服務(wù)

    使用Go?goroutine實現(xiàn)并發(fā)的Clock服務(wù)

    這篇文章主要為大家詳細(xì)介紹了如何使用Go?goroutine實現(xiàn)并發(fā)的Clock服務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • golang使用iconv報undefined:XXX的問題處理方案

    golang使用iconv報undefined:XXX的問題處理方案

    這篇文章主要介紹了golang使用iconv報undefined:XXX的問題處理方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論