Golang(又稱(chēng)Go語(yǔ)言)是一門(mén)現(xiàn)代化的編程語(yǔ)言,它是由 Google 設(shè)計(jì)和維護(hù)的。Golang 是一種靜態(tài)語(yǔ)言,它通過(guò)強(qiáng)類(lèi)型和嚴(yán)格類(lèi)型檢查的方式來(lái)提高代碼的可維護(hù)性和健壯性。其中一個(gè)最有趣的特性就是接口,本文將會(huì)深入探討 Golang 接口的實(shí)現(xiàn)。
Golang 接口是什么
和C++、Java等其他面向?qū)ο缶幊陶Z(yǔ)言一樣,Golang 也支持接口(interface)。在Go中,接口是一組方法的聲明,接口類(lèi)型是必須實(shí)現(xiàn)這些方法的類(lèi)型的集合。簡(jiǎn)單來(lái)說(shuō),接口是一種抽象的類(lèi)型,它定義了方法簽名但不包括實(shí)現(xiàn)。接口可以包含0個(gè)或多個(gè)方法簽名,不包含任何域。
接口可以比作一張卡片,卡片上寫(xiě)著規(guī)定了某些使用規(guī)則,這樣實(shí)現(xiàn)此卡片接口的人就知道了他必須要遵循這些規(guī)則進(jìn)行編碼,這樣就使得編碼的便捷性和代碼的靈活性得到了提高。
接口的實(shí)現(xiàn)
在 Golang 中實(shí)現(xiàn)接口非常簡(jiǎn)單,一個(gè)類(lèi)型只需要實(shí)現(xiàn)了接口類(lèi)型中所有的方法,它就可以稱(chēng)為這個(gè)接口類(lèi)型的實(shí)現(xiàn)類(lèi)型。定義實(shí)現(xiàn)一個(gè)接口的過(guò)程如下:
type interfaceName interface{ method1(param1 type1, param2 type2, ...) (return1 type1, return2 type2, ...) method2(param1 type1, ...) (return1 type1, ...) ... } type myType struct { // myType 的一些域 } func (t *myType) method1(param1 type1, param2 type2, ...) (return1 type1, return2 type2, ...) { // 方法體 } func (t *myType) method2(param1 type1, ...) (return1 type1, ...) { // 方法體 } // ...
在上面的代碼中,我們定義了一個(gè)接口 interfaceName,它包含了若干個(gè)方法。接著我們定義了一個(gè)結(jié)構(gòu)體 myType,該結(jié)構(gòu)體有一些屬性,然后我們分別實(shí)現(xiàn)了 interfaceName 中的方法。這樣 myType 就成為了 interfaceName 的實(shí)現(xiàn)類(lèi)型。
需要注意的是,接口的實(shí)現(xiàn)是非侵入式的,也就是說(shuō),我們無(wú)需修改已定義好的類(lèi)型,只需定義出要實(shí)現(xiàn)的方法即可。
接口類(lèi)型和實(shí)現(xiàn)類(lèi)型之間的關(guān)系
一個(gè)接口類(lèi)型可以建立多個(gè)實(shí)現(xiàn)類(lèi)型。也就是說(shuō),一個(gè)接口類(lèi)型可以被多個(gè)類(lèi)型所實(shí)現(xiàn)。下面是一個(gè)例子:
type Animal interface { Move() string } type Dog struct {} func (d Dog) Move() string { return "I'm a dog, I can walk on 4 legs" } type Bird struct {} func (b Bird) Move() string { return "I'm a bird, I can fly" } func main() { d := new(Dog) b := new(Bird) animal1 := Animal(d) animal2 := Animal(b) fmt.Println(animal1.Move()) fmt.Println(animal2.Move()) }
在上面的例子中,我們定義了一個(gè) Animal 接口和兩種類(lèi)型的實(shí)現(xiàn):Dog 和 Bird。接著我們創(chuàng)建了一個(gè) animal1 和 animal2 對(duì)象,它們的類(lèi)型都是 Animal,但是指向的實(shí)際類(lèi)型分別是 Dog 和 Bird。最后在 main 函數(shù)中分別調(diào)用了animal1 和 animal2 的 Move() 方法,并按實(shí)際類(lèi)型的行為輸出它們的移動(dòng)方式。
我們可以看到實(shí)際類(lèi)型的 Move() 方法實(shí)現(xiàn)了接口類(lèi)型的 Move() 方法,只要是實(shí)現(xiàn)了接口的類(lèi)型,就可以被稱(chēng)為實(shí)現(xiàn)類(lèi)型。
空接口
Golang中的空接口interface {}是一種特殊的接口,它沒(méi)有任何方法,可以表示任何類(lèi)型,相當(dāng)于java中的Object類(lèi)??梢员挥脕?lái)定義任意類(lèi)型的參數(shù)或返回值,比如:
func foo(a interface{}) { switch a.(type){ case string: fmt.Println("this is string type") case int: fmt.Println("this is int type") default: fmt.Println("unknown type") } } func main() { foo("hello") foo(42) foo(3.14159) }
在上面的例子中,我們定義了一個(gè) foo 函數(shù),它的參數(shù)是一個(gè)空接口類(lèi)型a。我們使用了一個(gè)switch語(yǔ)句來(lái)判斷a實(shí)際的類(lèi)型,并作出相應(yīng)的反應(yīng)??梢钥吹?,空接口可以接收任何類(lèi)型的參數(shù)。
總結(jié)
Golang 接口的實(shí)現(xiàn)是一件非常簡(jiǎn)單的事情。只要一個(gè)類(lèi)型實(shí)現(xiàn)了一個(gè)接口類(lèi)型中的所有方法,它就被稱(chēng)作這個(gè)接口類(lèi)型的實(shí)現(xiàn)類(lèi)型。一個(gè)接口類(lèi)型可以有多個(gè)實(shí)現(xiàn)類(lèi)型。空接口是一種特殊的接口,它沒(méi)有任何方法,可以表示任何類(lèi)型。此外,接口是非侵入式的,這使得編寫(xiě)代碼的靈活性和易讀性得到了極大的提高。