【发布时间】:2015-03-23 08:21:49
【问题描述】:
假设我必须实现在两个不同的包中声明的两个不同的接口(在两个不同的独立项目中)。
我在包里有A
package A
type interface Doer {
Do() string
}
func FuncA(Doer doer) {
// Do some logic here using doer.Do() result
// The Doer interface that doer should implement,
// is the A.Doer
}
在包中B
package B
type interface Doer {
Do() string
}
function FuncB(Doer doer) {
// some logic using doer.Do() result
// The Doer interface that doer should implement,
// is the B.Doer
}
在我的main 包中
package main
import (
"path/to/A"
"path/to/B"
)
type C int
// this method implement both A.Doer and B.Doer but
// the implementation of Do here is the one required by A !
func (c C) Do() string {
return "C now Imppement both A and B"
}
func main() {
c := C(0)
A.FuncA(c)
B.FuncB(c) // the logic implemented by C.Do method will causes a bug here !
}
如何处理这种情况?
【问题讨论】:
-
没有什么可处理的:Any 类型有一个方法
Do() string实现both 接口A.Doer和B.Doer。 -
我认为你是对的@Volker,没有解决办法,这种情况可能发生在任何使用接口的语言中(例如
java)。