第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

交換接口實(shí)現(xiàn),無(wú)開(kāi)銷

交換接口實(shí)現(xiàn),無(wú)開(kāi)銷

Go
胡說(shuō)叔叔 2021-05-15 15:08:25
給定一個(gè)接口和兩個(gè)(或更多)實(shí)現(xiàn),在擴(kuò)展功能時(shí),我很難輕松地切換實(shí)現(xiàn)。例如,假設(shè)有一個(gè)接口INumber支持Inc和String以及兩個(gè)實(shí)現(xiàn)NumberInt32和NumberInt64及其明顯的實(shí)現(xiàn)。假設(shè)我想在INumber之上實(shí)現(xiàn)一個(gè)EvenCounter。EvenCounter僅具有一個(gè)IncTwice,并且應(yīng)兩次致電Inc。我很難在不使用 EvenCounter 中 INumber 周圍的額外結(jié)構(gòu)的情況下獲得正確的類型。type INumber interface {    Inc()     String() string}type NumberInt32 struct {    number int32}func NewNumberInt32() INumber {    ret := new(NumberInt32)    ret.number = 0    return ret}func (this *NumberInt32) Inc() {     this.number += 1}func (this *NumberInt32) String() string {    return fmt.Sprintf("%d", this.number)}// type NumberInt64.... // obvious這是我掙扎的地方type EvenCounter1 INumber // nope, additional methods not possible type EvenCounter2 NumberInt32 // nopefunc (this *EvenCounter2) IncTwice() {  for i:=0; i < 2; i+=1 {    // this.Inc() // Inc not found    // INumber(*this).Inc() // cannot convert           // in, ok := *this.(INumber) // cannot convert    // v, ok := this.(INumber) // cannot convert    // a concrete conversion a) does not work and b) won't help    // here it should be generic    // v, ok := this.(NumberInt32)     // How do I call Inc here on this?    }}只是嵌入一個(gè)結(jié)構(gòu)作品...type EvenCounter3 struct {    n INumber}func (this *EvenCounter3) IncTwice() {    n := this.n // that is a step I want to avoid    n.Inc() // using this.n.Inc() twice makes it slower    n.Inc()}func (this *EvenCounter3) String() string {    return this.n.String()}我可以滿足為每種方法手動(dòng)實(shí)現(xiàn)委派的需要,但是顯然我想依賴INumber而不是特定的實(shí)現(xiàn)(這意味著要更改很多地方才能嘗試另一個(gè)實(shí)現(xiàn),但是,我想避免多余的操作是否有間接方法和(最有可能?)額外的空間,有沒(méi)有一種方法可以避免使用該結(jié)構(gòu),而直接說(shuō)EvenCounter是帶有其他方法的(特定的)INumber?順便說(shuō)一句,真正的例子是一組整數(shù)和一個(gè)整數(shù)到整數(shù)的映射,這些整數(shù)具有數(shù)百萬(wàn)個(gè)實(shí)例,所有實(shí)例都交織在一起(而且不行,僅map [int] bool是不夠的-太慢了,根據(jù)用例而定,bitset很有趣,依此類推。 ),并通過(guò)更改代碼中的2-3行來(lái)輕松測(cè)試集合和映射的不同實(shí)現(xiàn)(理想情況下只是類型,并且可能是實(shí)例的通用創(chuàng)建或復(fù)制)任何幫助表示贊賞,我希望這還沒(méi)有被要求...
查看完整描述

2 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

您使用嵌入的變體實(shí)際上并未嵌入。嵌入字段是匿名的,然后Go會(huì)自動(dòng)委托。


這將您的示例簡(jiǎn)化為:


type EvenCounter3 struct {

    INumber

}


func (this *EvenCounter3) IncTwice() {

    this.Inc() // using this.n.Inc() twice makes it slower

    this.Inc()

}

請(qǐng)注意,String()是自動(dòng)委派的(在Go語(yǔ)言中為“提升”)。


至于兩次調(diào)用Inc()會(huì)使它變慢,那是使用接口的限制。接口的重點(diǎn)是不公開(kāi)實(shí)現(xiàn),因此您無(wú)法訪問(wèn)其內(nèi)部數(shù)字變量。


查看完整回答
反對(duì) 回復(fù) 2021-05-31
?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

我不確定我是否正確理解您要達(dá)到的目標(biāo)。也許是這樣的?


package main


import "fmt"


type Num interface {

        Inc()

        String() string

}


type Int32 struct {

        int32

}


func (n *Int32) Inc() {

        (*n).int32++

}


func (n Int32) String() string {

        return fmt.Sprintf("%d", n.int32)

}


type EventCounter interface {

        Num

        IncTwice()

}


type Event struct {

        Num

}


func (e Event) IncTwice() {

        e.Inc()

        e.Inc()

}


func main() {

        e := Event{&Int32{42}}

        e.IncTwice()

        fmt.Println(e)

}

(在這里)


輸出


44


查看完整回答
反對(duì) 回復(fù) 2021-05-31
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)