golang 接口嵌套實(shí)現(xiàn)復(fù)用的操作
大家還是直接看代碼吧~
package main import ( "fmt" ) func main() { start(NewB(C{})) start(NewB(D{})) } type A interface { what() } type B struct { A } type C struct { } func (b C) what() { fmt.Println("this is type C") } type D struct { } func (b D) what() { fmt.Println("this is type D") } func start(b B) { b.what() } func NewB(a A) B { return B{a} }
補(bǔ)充:【玩轉(zhuǎn)Golang】通過組合嵌入實(shí)現(xiàn)代碼復(fù)用
應(yīng)用開發(fā)中的一個(gè)常見情景,為了避免簡(jiǎn)單重復(fù),需要在基類中實(shí)現(xiàn)共用代碼,著同樣有助于后期維護(hù)。
如果在以往的支持類繼承的語言中,比如c++,Java,c#等,這很簡(jiǎn)單!可是go不支持繼承,只能mixin嵌入
且看下面的代碼:
type ManKind interface{ Say(s string); GetMouth()string } type Man struct{ } func NewMan() ManKind{ return &Man{}; } func (this *Man)GetMouth()string{ return "M0" } func (this *Man) Say(s string){ fmt.Printf("\n Speak with mouth[%s] : \"%s\"",this.GetMouth(),s); } type StrongMan struct{ Man } func NewStrongMan()ManKind{ return &StrongMan{} } func (this*StrongMan)GetMouth()string{ return "M1" } func main(){ NewMan().Say("good luck!") NewStrongMan().Say("good luck!") }
如果支持繼承,很明顯應(yīng)該輸出
Speak with mouth[M0] : "good luck!"
Speak with mouth[M1] : "good luck!"
但是在golang中只能輸出:
Speak with mouth[M0] : "good luck!"
Speak with mouth[M0] : "good luck!"
StrongMan中調(diào)用Say(),此時(shí)可以將指針傳遞到內(nèi)嵌類,只是簡(jiǎn)單的指向了Man的方法,在ManKind中調(diào)用GetMouth就是ManKind自己的GetMouth,和StrongMan沒有關(guān)系。
當(dāng)然,我們可以在StrongMan中覆蓋Say方法
func (this *StrongMan)Say(s string){ fmt.Printf("\n Speak with mouth[%s] : \"%s\"",this.GetMouth(),s); }
此時(shí),當(dāng)然可以正確輸出,因?yàn)楸緛碚{(diào)用的就都是StrongMan自己的方法了,這又和我們的初衷相違背了。那么這種情況怎么實(shí)現(xiàn)呢?我的方法是,讓Man再臟一點(diǎn)兒,把需要的東西傳遞給組合進(jìn)來的類。
給Man增加一個(gè)屬性mouth,增加一個(gè)SetMouth方法,修改一下GetMouth方法,StrongMan的GetMouth方法刪除掉,再修改一下NewStrongMan方法
最后的代碼如下:
package main import( "fmt" ) type ManKind interface{ Say(s string); SetMouth(m string) GetMouth()string } type Man struct{ ManKind mouth string } func NewMan() ManKind{ return &Man{mouth:"M0"}; } func (this *Man)GetMouth()string{ return this.mouth; } func (this *Man)SetMouth(s string){ this.mouth=s; } func (this *Man) Say(s string){ fmt.Printf("\n Speak with mouth[%s] : \"%s\"",this.GetMouth(),s); } type StrongMan struct{ Man } func NewStrongMan()ManKind{ sm := &StrongMan{} sm.SetMouth("M1"); return sm; } func main(){ NewMan().Say("good luck!") &NewStrongMan().Say("good luck!") }
當(dāng)然,如果你不愿意用Get、Set方法,也可以直接輸出Man的Mouth屬性。
我總結(jié)的嵌入式編程要點(diǎn):
1,被嵌入的類的方法,只能訪問他自己的字段,包裝類即時(shí)聲明了同名字段也沒用。
2,包裝類可以覆蓋嵌入類的方法,但是嵌入類訪問不到,亦然訪問自己的方法。只能在包裝類中連同調(diào)用方法一同實(shí)現(xiàn)。
3,包裝類覆蓋嵌入類字段后,亦然可以通過嵌入類的類名訪問嵌入類的字段。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Go語言-為什么返回值為接口類型,卻返回結(jié)構(gòu)體
這篇文章主要介紹了Go語言返回值為接口類型,卻返回結(jié)構(gòu)體的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04GoLang并發(fā)機(jī)制探究goroutine原理詳細(xì)講解
goroutine是Go語言提供的語言級(jí)別的輕量級(jí)線程,在我們需要使用并發(fā)時(shí),我們只需要通過 go 關(guān)鍵字來開啟 goroutine 即可。這篇文章主要介紹了GoLang并發(fā)機(jī)制goroutine原理,感興趣的可以了解一下2022-12-12Golang在整潔架構(gòu)基礎(chǔ)上實(shí)現(xiàn)事務(wù)操作
這篇文章在 go-kratos 官方的 layout 項(xiàng)目的整潔架構(gòu)基礎(chǔ)上,實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)庫事務(wù)操作,需要的朋友可以參考下2024-08-08