dsafyx8454
dsafyx8454
2017-11-12 07:54
浏览 180
已采纳

我如何有golang接口实现返回另一个接口的实现?

Specific example is here: https://play.golang.org/p/ZNmviKWLwe

I have an interface A. I expect all implementations of A to have a certain signature (hence the interface). I expect one of those to return something useful that implements another interface.

type Box interface {
    GetToy(int) (*Toy, error)
}
type Toy interface{
    Play()
}


type CarBox struct {
   Length int
   Width int
   Height int
   toys []*Matchbox
}
type Matchbox struct {}

func (b *CarBox) Size () int {
    return b.Length*b.Width*b.Height
}
func (b *CarBox) GetToy(j int) (*Matchbox, error) {
    return b.toys[j], nil
}
func (m *Matchbox) Play() {
    fmt.Println("Zoom!")
}

But of course, it doesn't work, because GetToy() returns *Matchbox and not *Toy, even though Matchbox is a perfectly acceptable implementation of Toy. And, no, it isn't because of the pointers; it gives the same result whether I return *Toy in GetToy or just Toy:

tmp/sandbox721971102/main.go:33:15: cannot use CarBox literal (type CarBox) as type Box in return argument:
    CarBox does not implement Box (wrong type for GetToy method)
        have GetToy(int) (*Matchbox, error)
        want GetToy(int) (*Toy, error)

Obviously my pattern doesn't match how go works. What is the "correct" way to go about doing this?

图片转代码服务由CSDN问答提供 功能建议

具体示例在此处: https://play.golang.org/p/ZNmviKWLwe

我有一个接口A。我希望A的所有实现都具有一个 某些签名(因此是接口)。 我希望其中一个能够返回有用的东西,以实现另一个接口。

  type Box接口{
 GetToy(int)(* Toy,error)
} 
type Toy 接口{
 Play()
} 
 
 
type CarBox结构{
长度int 
宽度int 
高度int 
玩具[] * Matchbox 
} 
type Matchbox结构{} 
 \  nfunc(b * CarBox)Size()int {
返回b.Length * b.Width * b.Height 
} 
func(b * CarBox)GetToy(j int)(* Matchbox,error){
返回 b.toys [j],nil 
} 
func(m * Matchbox)Play(){
 fmt.Println(“ Zoom!”)
} 
   
 
  

但是,它当然不起作用,因为即使 GetToy()返回 * Matchbox 而不是 * Toy > Matchbox Toy 的完全可以接受的实现。 而且,不,不是因为指针。 无论我在 GetToy 中返回 * Toy 还是仅仅返回 Toy ,它都会给出相同的结果:

  tmp / sandbox721971102 / main.go:33:15:不能在返回参数中使用CarBox文字(CarBox类型)作为Box类型:
 CarBox没有实现Box(GetToy方法的类型错误)
有GetToy(int)(  * Matchbox,错误)
要GetToy(int)(* Toy,错误)
   
 
 

很明显,我的模式与go的工作方式不符。 执行此操作的“正确”方法是什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhangji4824
    dongzhangji4824 2017-11-12 08:09
    已采纳

    First of all you almost never need a pointer to an interface type. So GetToy method signature should be changed to:

    type Box interface {
        GetToy(int) (Toy, error) // use Toy, not *Toy
    }
    

    Now, note that interfaces are satisfied by types implicitly. As a result, by implementing the Play() method, *Matchbox implicitly satisfies the Toy interface (note the *, the type implementing the Toy interface is pointer to Matchbox and not Matchbox).

    This means a value of type *Matchbox can be substituted for type Toy. So what remains is to fix the GetToy method defined on *CarBox:

    func (b *CarBox) GetToy(j int) (Toy, error) {
        return b.toys[j], nil
    }
    

    Now GetToy above will return a *Matchbox which does implement the Toy interface.

    点赞 评论
  • dongnaosuan5407
    dongnaosuan5407 2017-11-12 08:33

    Some fixes: https://play.golang.org/p/-mDr5SDZvV

    Pointer to interface is extremely rearly required thing. Use interface - it can contain a concrete type.

    UPDATE: If you wish to have a method with pointer receiver like func (b *CarBox) GetToy(j int) you may return pointer from Tester function (pay attention to &):

    func Tester() Box {
        return &CarBox{}
    }
    

    This way interface value returned from Tester will conclude a pointer to the CarBox instance. Thus Go runtime will be able to run methods with pointer receiver. Otherwise there would be only a value not a pointer. So only methods with value receivers could be called.

    点赞 评论

相关推荐