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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未實現(xiàn),方法類型錯誤

未實現(xiàn),方法類型錯誤

Go
ibeautiful 2023-03-21 14:28:54
給定以下 Go 代碼:package maintype CatToy interface {    Rattle() string}type Cat struct {}func (cat *Cat) Play(catToy CatToy) {    println("The cat is playing!", catToy.Rattle())}type DogToy interface {    Roll() string}type Dog struct {}func (dog *Dog) Play(dogToy DogToy) {    println("The dog is playing!", dogToy.Roll())}type SuperToy struct {}func (toy *SuperToy) Rattle() string {    return "Rattle!!!"}func (toy *SuperToy) Roll() string {    return "Rolling..."}type Pet interface {    Play(toy interface{})}func main() {    cat := &Cat{}    dog := &Dog{}    superToy := &SuperToy{}    // Working    cat.Play(superToy)    dog.Play(superToy)    // Not Working    pets := []Pet{cat, dog}    for _, pet := range pets {        pet.Play(superToy)    }}我收到這些錯誤:# command-line-arguments./main.go:65:16: cannot use cat (type *Cat) as type Pet in array or slice literal:    *Cat does not implement Pet (wrong type for Play method)        have Play(CatToy)        want Play(interface {})./main.go:65:21: cannot use dog (type *Dog) as type Pet in array or slice literal:    *Dog does not implement Pet (wrong type for Play method)        have Play(DogToy)        want Play(interface {})實現(xiàn)SuperToy了CatToy和DogToy。但是,當我創(chuàng)建一個接口Pet作為參數(shù)的接口時,我得到一個錯誤。我可以知道我怎樣才能得到一個里面有貓和狗的數(shù)組/切片嗎?我想遍歷這個切片并為每個切片調(diào)用一個函數(shù)。我還想保留CatToy和DogToy接口。我也可以刪除Pet界面。更多信息:未來我更有可能添加更多pets。我認為我不會添加更多操作,例如Play.
查看完整描述

3 回答

?
萬千封印

TA貢獻1891條經(jīng)驗 獲得超3個贊

我明白你想做什么,但這是不可能的:你的Cat和Dog類型沒有實現(xiàn)Pet接口,因為它們的Play方法采用不同的類型,所以你不能只用Play你的SuperToy.


要解決此問題,您需要創(chuàng)建一個Toy接口,該接口同時具有Roll和Rattle方法以及 make Pet.Play,Cat.Play并將Dog.Play此接口作為參數(shù)。


package main


type Cat struct {

}


func (cat *Cat) Play(catToy Toy) {

    println("The cat is playing!", catToy.Rattle())

}


type Dog struct {

}


func (dog *Dog) Play(dogToy Toy) {

    println("The dog is playing!", dogToy.Roll())

}


type Toy interface {

    Roll() string

    Rattle() string

}


type SuperToy struct {

}


func (toy *SuperToy) Rattle() string {

    return "Rattle!!!"

}


func (toy *SuperToy) Roll() string {

    return "Rolling..."

}


type Pet interface {

    Play(toy Toy)

}


func main() {

    cat := &Cat{}

    dog := &Dog{}

    superToy := &SuperToy{}


    // Working

    cat.Play(superToy)

    dog.Play(superToy)


    // Not Working

    pets := []Pet{cat, dog}

    for _, pet := range pets {

        pet.Play(superToy)

    }

}

給出輸出


The cat is playing! Rattle!!!

The dog is playing! Rolling...

The cat is playing! Rattle!!!

The dog is playing! Rolling...


查看完整回答
反對 回復 2023-03-21
?
皈依舞

TA貢獻1851條經(jīng)驗 獲得超3個贊

您可以讓 Play 方法接受一個interface{},然后在方法內(nèi)進行類型斷言:


func (dog *Dog) Play(toy interface{}) {

    dogToy, isDogToy := toy.(DogToy)

    if !isDogToy {

        println("The dog does not know what to do with this toy!")

        return

    }

    println("The dog is playing!", dogToy.Roll())

}

Go Playground 上的完整可執(zhí)行示例:


https://play.golang.org/p/LZZ-HqpzR-Z


查看完整回答
反對 回復 2023-03-21
?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

這是另一個可行的解決方案:


package main


type CatToy interface {

? ? Rattle() string

}


type Cat struct {

? ? Toy CatToy

}


func (cat *Cat) Play() {

? ? println("The cat is playing!", cat.Toy.Rattle())

}


type DogToy interface {

? ? Roll() string

}


type Dog struct {

? ? Toy DogToy

}


func (dog *Dog) Play() {

? ? println("The dog is playing!", dog.Toy.Roll())

}


type SuperToy struct {

}


func (toy *SuperToy) Rattle() string {

? ? return "Rattle!!!"

}


func (toy *SuperToy) Roll() string {

? ? return "Rolling..."

}


type Pet interface {

? ? Play()

}


func main() {

? ? superToy := &SuperToy{}

? ? cat := &Cat{superToy}

? ? dog := &Dog{superToy}


? ? // Working

? ? cat.Play()

? ? dog.Play()


? ? // Working also

? ? pets := []Pet{cat, dog}

? ? for _, pet := range pets {

? ? ? ? pet.Play()

? ? }

}

該解決方案使Cat + CatToy和Dog + DogToy獨立于main + SuperToy. 這允許提取以分離包。


查看完整回答
反對 回復 2023-03-21
  • 3 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號