Dato la seguente configurazione:Java generici Enum subtyping interfaccia
public class TestType {
public static void main(String[] args) {
List<Constants> list = new ArrayList<>();
accept(list); //Does not compile
}
static void accept(Iterable<MyInterface> values) {
for (MyInterface value : values) {
value.doStuff();
}
}
}
interface MyInterface<T> {
T doStuff();
}
enum Constants implements MyInterface<Integer> {
ONE, TWO, THREE;
@Override
public Integer doStuff() {
return ordinal();
}
}
Perché non sarà il compilatore accettare l'elenco come parametro di accettare()?
List
estende Iterable
tramite Collection
in modo che non è il problema.
D'altra parte, il compilatore mi dice che incompatible types: java.util.List<enums.Constants> cannot be converted to java.lang.Iterable<enums.MyInterface>
Ma Costanti è un MyInterface ... non è vero?
Che funziona davvero. Accetterò la risposta non appena SO, lasciami. Molte grazie! – tannerli