Wygląda na to, że ponownie utknąłem w generycznych elementach java. Oto co mam:Niezgodne klasy generyczne języka Java
Kilka klas:
class CoolIndex implements EntityIndex<CoolEntity>
class CoolEntity extends BaseEntity
Enum pomocą klas powyżej:
enum Entities {
COOL_ENTITY {
@Override
public <E extends BaseEntity, I extends EntityIndex<E>> Class<I> getIndexCls() {
return CoolIndex.class;
}
@Override
public <E extends BaseEntity> Class<E> getEntityCls() {
return CoolEntity.class;
}
}
public abstract <E extends BaseEntity, I extends EntityIndex<E>> Class<I> getIndexCls();
public abstract <E extends BaseEntity> Class<E> getEntityCls();
}
Funkcja Muszę zadzwonić z wykorzystaniem wyniku getIndexCls()
wywołanie funkcji:
static <E extends BaseEntity, I extends EntityIndex<E>> boolean isSomeIndexViewable(Class<I> cls)
Problemem jest to, że kompilator narzeka return CoolIndex.class;
i return CoolEntity.class;
i to nie jest dla mnie jasne, dlaczego ... Oczywiście mogę rzucić go do Class<I>
(pierwszy przypadek), ale wydaje mi się, jakbym próbował zamaskować mój nieporozumienie i nie czuje się dobrze.
Powiązane (może pomóc): http://stackoverflow.com/q/20973221/1225328 – sp00m
Kilka innych strategii: http://stackoverflow.com/questions/11490485/how-to-implement-enum- with-generics – azurefrog
Bardzo podobny problem do http://stackoverflow.com/questions/28234960/java-generics i-enum-loss-of-template-parameters – EpicPandaForce