我对 Java 中的泛型编程有一个小问题。有没有办法检查传递给方法的参数的类型?我想比较调用该方法的实例的类型以及传递给它的参数。如果它们不相同,则应停止该方法的操作(保护种类)。public void homeMatch(SportTeam<Type> visitors){ if(the type of visitors and this-object are not the same){ //do this } else{ //do something different }}
3 回答

守着一只汪
TA贡献1872条经验 获得超3个赞
如果您真的想要进行检查,您可以做的是将类参数添加到函数参数中。然后将 classParameter 与 this 的类进行比较。这会起作用,因为访问者具有与 typeClass 相同的泛型类型。
public<Type> void homeMatch(Class<Type> typeClass, SportTeam<Type> visitors){
if (typeClass.getClass() == this.getClass()){
}
}
添加回答
举报
0/150
提交
取消