Supponiamo che io debba implementare due diverse interfacce dichiarate in due pacchetti diversi (in due diversi progetti separati).Come implementare due interfacce diverse con lo stesso metodo di firma
ho nel pacchetto 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
}
E nel pacchetto 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
}
Nel mio pacchetto 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 !
}
Come affrontare questa situazione?
Non c'è assolutamente nulla da trattare: ** Qualsiasi ** tipo che ha un metodo 'Do() stringa' implementa * entrambe * interfacce' A.Doer' e 'B.Doer'. – Volker
Penso che tu abbia ragione @Volker, non c'è una soluzione per questo, e questa situazione può verificarsi in qualsiasi linguaggio che usi le interfacce ('java' per esempio). – tarrsalah