关于接口的组合
来源: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。否则不行
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
}
浅弋璃鱼
2021-07-11
来来来, 把你的代码贴上来看一看
相似问题