因此,在阅读了这篇文章之后,我找到了这个特定问题的解决方案。
https://docs.oracle.com/javase/tutorial/java/generics/capture.html
问题是,当使用通配符时,编译器无法保证对象的类型正确,但通过使用通用助手方法,您可以通过捕获通配符的类型来确保每个元素的类型安全。在本例中,这是我使用的助手方法。
private <T extends Component> void addNewComponentInstance(Class<T> type, int uid){
try {
getParent().getComponentManager().add(type, uid, type.newInstance());
} catch (Exception ex) {
ex.printStackTrace();
}
}
这就是它是如何被夹在通配符方法中的。
public Entity createEntity(Class<? extends Component>... types){
Entity e = new Entity();
for(Class<? extends Component> type: types)
addNewComponentInstance(type, e.UID);
INDEX.add(e.UID, e);
return e;
}
此解决方案可正确编译和运行。