您好,登錄后才能下訂單哦!
1.定義
Interface是類型可以定義一組方法,但是這些不需要實現。并且interface不能包含任何變量。
比如:
type example interface{
Method1(參數列表) 返回值列表
Method2(參數列表) 返回值列表
…
}
2.interface類型默認是一個指針
type example interface{
Method1(參數列表) 返回值列表
Method2(參數列表) 返回值列表
…
}
var a example
a.Method1()
3.接口實現
a. Golang中的接口,不需要顯示的實現。只要一個變量,含有接口類型中的所有方法,那么這個變量就實現這個接口。因此,golang中沒有implement類似的關鍵字
b. 如果一個變量含有了多個interface類型的方法,那么這個變量就實現了多個接口。
c. 如果一個變量只含有了1個interface的方部分方法,那么這個變量沒有實現這個接口。
4.多態
一種事物的多種形態,都可以按照統一的接口進行操作
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) //轉成int
var t int
var x interface{}
x = t
y, ok = x.(int) //轉成int,帶檢查
7.練習,寫一個函數判斷傳入參數的類型
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{}
空接口沒有任何方法,所以所有類型都實現了空接口。
var a int
var b interface{}
b = a
10.判斷一個變量是否實現了指定接口
判斷一個變量是否實現了指定接口
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"
//接口里的方法必須全部實現!
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
//如果接口中的方法只聲明不實現,則是nil
//fmt.Println(car)
//不實現Run方法而調用會報錯
//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"
//接口里的方法必須全部實現!
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
//如果接口中的方法只聲明不實現,則是nil
//fmt.Println(car)
//不實現Run方法而調用會報錯
//car.Run()
var bmw BMW
bmw.Name = "x6"
//值類型傳遞也是可以的,上面的p *BMW也要換成p BMW
car = bmw
car.Run()
}
應用實例:
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("")
//調用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)是數據類型的轉換,這是接口轉成具體類
b, ok := a.(int)
if ok == false {
fmt.Println("轉換失敗")
return
}
b += 3
fmt.Println(b)
}
func main() {
//b是int型則轉換成功,string型轉換失敗
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。