1.定義
Interface是類型可以定義一組方法,但是這些不需要實現(xiàn)。并且interface不能包含任何變量。
比如:
type example interface{
Method1(參數(shù)列表) 返回值列表
Method2(參數(shù)列表) 返回值列表
…
}
2.interface類型默認是一個指針
type example interface{
Method1(參數(shù)列表) 返回值列表
Method2(參數(shù)列表) 返回值列表
…
}
var a example
a.Method1()
3.接口實現(xiàn)
a. Golang中的接口,不需要顯示的實現(xiàn)。只要一個變量,含有接口類型中的所有方法,那么這個變量就實現(xiàn)這個接口。因此,golang中沒有implement類似的關(guān)鍵字
b. 如果一個變量含有了多個interface類型的方法,那么這個變量就實現(xiàn)了多個接口。
c. 如果一個變量只含有了1個interface的方部分方法,那么這個變量沒有實現(xiàn)這個接口。
4.多態(tài)
一種事物的多種形態(tài),都可以按照統(tǒng)一的接口進行操作
5.接口嵌套
一個接口可以嵌套在另外的接口,如下所示:
type ReadWrite interface {
Read(b Buffer) bool
Write(b Buffer) bool
}
type Lock interface {
Lock()
Unlock()
}
type File interface {
ReadWrite
Lock
Close()
}
var t int
var x interface{}
x = t
y = x.(int) //轉(zhuǎn)成int
var t int
var x interface{}
x = t
y, ok = x.(int) //轉(zhuǎn)成int,帶檢查
7.練習(xí),寫一個函數(shù)判斷傳入?yún)?shù)的類型
func classifier(items ...interface{}) {
for i, x := range items {
switch x.(type) {
case bool: fmt.Printf(“param #%d is a bool\n”, i)
case float64: fmt.Printf(“param #%d is a float64\n”, i)
case int, int64: fmt.Printf(“param #%d is an int\n”, i)
case nil: fmt.Printf(“param #%d is nil\n”, i)
case string: fmt.Printf(“param #%d is a string\n”, i)
default: fmt.Printf(“param #%d’s type is unknown\n”, i)
}
}
8.類型斷言,采用type switch方式
switch t := areaIntf.(type) {case *Square: fmt.Printf(“Type Square %T with value %v\n”, t, t)
case *Circle: fmt.Printf(“Type Circle %T with value %v\n”, t, t)
case float32: fmt.Printf(“Type float32 with value %v\n”, t)case nil: fmt.Println(“nil value: nothing to check?”)
default: fmt.Printf(“Unexpected type %T”, t)}
9.空接口,Interface{}
空接口沒有任何方法,所以所有類型都實現(xiàn)了空接口。
var a int
var b interface{}
b = a
10.判斷一個變量是否實現(xiàn)了指定接口
判斷一個變量是否實現(xiàn)了指定接口
type Stringer interface {
String() string
}
var v MyStruct
if sv, ok := v.(Stringer); ok {
fmt.Printf(“v implements String(): %s\n”, sv.String());
}
package main
import "fmt"
//沒有方法的空接口,任何類型都可以
type Test interface {}
func main() {
var a interface{}
a = 666
fmt.Println(a)
fmt.Printf("a的類型是:%T", a)
}
輸出:
666
a的類型是:int
Process finished with exit code 0
package main
import "fmt"
//接口里的方法必須全部實現(xiàn)!
type Carer interface {
GetName() string
Run()
DiDi()
}
type BMW struct {
Name string
}
func (p *BMW) GetName() string {
return p.Name
}
func (p *BMW) Run() {
fmt.Printf("%s is running\n", p.Name)
}
func (p *BMW) DiDi() {
fmt.Printf("%s is didi\n", p.Name)
}
func main() {
var car Carer
//如果接口中的方法只聲明不實現(xiàn),則是nil
//fmt.Println(car)
//不實現(xiàn)Run方法而調(diào)用會報錯
//car.Run()
var bmw BMW
bmw.Name = "x6"
//因為struct BMW是指針類型,所以bmw要引用傳遞
car = &bmw
car.Run()
}
輸出:
x6 is running
Process finished with exit code 0
也可以值傳遞:
package main
import "fmt"
//接口里的方法必須全部實現(xiàn)!
type Carer interface {
GetName() string
Run()
DiDi()
}
type BMW struct {
Name string
}
func (p BMW) GetName() string {
return p.Name
}
func (p BMW) Run() {
fmt.Printf("%s is running\n", p.Name)
}
func (p BMW) DiDi() {
fmt.Printf("%s is didi\n", p.Name)
}
func main() {
var car Carer
//如果接口中的方法只聲明不實現(xiàn),則是nil
//fmt.Println(car)
//不實現(xiàn)Run方法而調(diào)用會報錯
//car.Run()
var bmw BMW
bmw.Name = "x6"
//值類型傳遞也是可以的,上面的p *BMW也要換成p BMW
car = bmw
car.Run()
}
應(yīng)用實例:
go語言的sort包是用接口方式寫的,可以輕松的自定義:
package main
import (
"fmt"
"math/rand"
"sort"
)
type Student struct {
Name string
Id string
Age int
}
type Book struct {
Name string
Author string
}
//切片默認就是傳地址的,所以不需要在方法中用*
type studentArray []Student
func (p studentArray) Len() int {
return len(p)
}
func (p studentArray) Less(i, j int) bool {
return p[i].Name < p[j].Name
}
func (p studentArray) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func main() {
var stus studentArray
for i := 0; i < 5; i++ {
stu := Student{
Name: fmt.Sprintf("stu%d", rand.Intn(100)),
Id: fmt.Sprintf("100%d", rand.Int()),
Age: rand.Intn(100),
}
stus = append(stus, stu)
}
for _, v := range stus {
fmt.Println(v)
}
fmt.Println("")
//調(diào)用sort包進行排序(stus中用自帶的方法)
sort.Sort(stus)
for _, v := range stus {
fmt.Println(v)
}
}
輸出:
{stu81 1008674665223082153551 47}
{stu59 1003916589616287113937 18}
{stu25 1001443635317331776148 56}
{stu0 1004751997750760398084 11}
{stu62 1003510942875414458836 28}
{stu0 1004751997750760398084 11}
{stu25 1001443635317331776148 56}
{stu59 1003916589616287113937 18}
{stu62 1003510942875414458836 28}
{stu81 1008674665223082153551 47}
Process finished with exit code 0
類型斷言實例:
package main
import "fmt"
func main() {
var a interface{}
fmt.Printf("%T\n", a)
var b int
a = b
c := a.(int)
fmt.Printf("%T\n", a)
fmt.Printf("%T\n", c)
}
輸出:
<nil>
int
int
Process finished with exit code 0
package main
import "fmt"
func duanYan(a interface{}) {
//int(a)是數(shù)據(jù)類型的轉(zhuǎn)換,這是接口轉(zhuǎn)成具體類
b, ok := a.(int)
if ok == false {
fmt.Println("轉(zhuǎn)換失敗")
return
}
b += 3
fmt.Println(b)
}
func main() {
//b是int型則轉(zhuǎn)換成功,string型轉(zhuǎn)換失敗
var b int
duanYan(b)
}
輸出:
3
Process finished with exit code 0
package main
import "fmt"
type Student struct {
Name string
Sex string
}
func Test(a interface{}) {
b, ok := a.(Student)
if ok == false {
fmt.Println("convert failed")
return
}
//b += 3
fmt.Println(b)
}
func just(items ...interface{}) {
for index, v := range items {
switch v.(type) {
case bool:
fmt.Printf("%d params is bool, value is %v\n", index, v)
case int, int64, int32:
fmt.Printf("%d params is int, value is %v\n", index, v)
case float32, float64:
fmt.Printf("%d params is float, value is %v\n", index, v)
case string:
fmt.Printf("%d params is string, value is %v\n", index, v)
case Student:
fmt.Printf("%d params student, value is %v\n", index, v)
case *Student:
fmt.Printf("%d params *student, value is %v\n", index, v)
}
}
}
func main() {
var b Student = Student{
Name: "stu01",
Sex: "female",
}
Test(b)
just(28, 8.2, "this is a test", b, &b)
}
輸出:
{stu01 female}
0 params is int, value is 28
1 params is float, value is 8.2
2 params is string, value is this is a test
3 params student, value is {stu01 female}
4 params *student, value is &{stu01 female}
Process finished with exit code 0
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
當前文章:接口/類型斷言-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://aaarwkj.com/article28/gjdcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)頁設(shè)計公司、微信小程序、搜索引擎優(yōu)化、商城網(wǎng)站、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)