我觉得我不得不在这里问这个问题,因为在其他地方找不到合适的答案。
我正在尝试使以下代码段在componentList
是List<Component>
的地方工作。我知道一些解决方法,例如评论here中提到的解决方法。但是我只是想知道Java中是否存在某种表达式来检查泛型参数的instanceof
而没有它的实例。我知道我也可以使用class
或某种值作为它的标识符。
public <T extends Component> T getcomponent(){
for (Component component:componentList) {
if(component instanceof T){
return component;
}
}
return null;
}
已经感谢您的回答和拼写检查^^