我在比较Java泛型类型时遇到了问题,主要是想确定我的泛型类型T是否为Void。以下是我样例代码的实现:
public abstract class Request<T>{
private T member;
protected void comparing(){
if(T instanceof Void) // this make error "Expression expected"
runAnotherMethod();
//if I type
if(member instanceof Void) //Incovertible types; cannot cast T to java.lang.Void
runAnotherMethod();
}
protected void runAnotherMethod(){...}
}
public class ParticularRequest extends Request<Void>{
}
我尝试使用 instanceof
、Class<T>
、Class<Void>
、T.class
和 Void.class
来比较 id,但是 AndroidStudio 在每种尝试中都显示错误 :(
你能帮我解决如何进行比较吗? 谢谢。