gpt4 book ai didi

java - 将 Integer.class 作为参数传递

转载 作者:行者123 更新时间:2023-11-29 06:30:41 26 4
gpt4 key购买 nike

我有以下类(class):

package query;

public class Predicate<T> {

private String operand1;
private String operator;
private T operand2;
private Class<T> classType;

public Predicate(String operand1, T operand2, Class<T> classType){
this(operand1, "=", operand2, classType);
}

public Predicate(String operand1, String operator, T operand2, Class<T> classType){
this.operand1 = operand1;
this.operator = operator;
this.operand2 = operand2;
this.classType = classType;
}

public String getOperand1() {
return operand1;
}

public String getOperator() {
return operator;
}

public T getOperand2() {
return operand2;
}

public Class<T> getClassType() {
return classType;
}

@Override
public String toString() {
return operand1 + " " + operator + " ?";
}
}

以下行可以正常编译:

Predicate<String> p1 = new Predicate<>("given_name", "=", "Kim", String.class); 

但是,下面的不是:

Predicate<Integer> p1 = new Predicate<>("id", "=", "1", Integer.class); 

编译器提到:Cannot infer type arguments for Predicate<>

出了什么问题/如何解决这个问题?

最佳答案

“1”是一个字符串,与 Integer.class 不兼容

new Predicate<>("id", "=", 1, Integer.class)

应该没问题

new Predicate<>("id", "=", "1", String.class)

关于java - 将 Integer.class 作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com