关于接口的组合

来源:1-5 接口的组合

慕移动9586716

2021-07-10 13:21:06

老师,当我们实现了接口A,接口B,然后再实现一个接口C(接口C是A、B的组合)当我们使用接口C时,能不能将接口A或者接口B的类型代入接口C中?


写回答

3回答

Morin110

2021-07-19

除非接口A和接口B都实现了接口C。否则不行

0

ccmouse

2021-07-16

我大概理解一下其中的意思:

type InterfaceA interface {

   MethodA()

}


type InterfaceB interface {

   MethodB()

}


type InterfaceC interface {

  InterfaceA

  InterfaceB

}


func Combine(a InterfaceA, b InterfaceB) InterfaceC {


}

请问combine函数如何实现?也是可以的:

func Combine(a InterfaceA, b InterfaceB) InterfaceC {

  c := struct {

    InterfaceA

    InterfaceB

  }{

    InterfaceA: a,

    InterfaceB: b,

  }

  return c

}


0
hq_永远的稻米_0
hp>老师,请问Combine函数怎么调用的呢?

h022-06-03
共1条回复

浅弋璃鱼

2021-07-11

来来来, 把你的代码贴上来看一看?

0

0 学习 · 1399 问题

查看课程