当使用Java中的泛型时,我有一个问题。 我试图提供一个解释我问题的小例子。
IVariable.java
public interface IVariable {
}
IProblem.java
public interface IProblem<V extends IVariable> {
}
Algorithm.java
public class Algorithm<V extends IVariable, P extends IProblem<V>> {
public void doSomething(P problem) {
// ERROR: Type mismatch: cannot convert from P to IProblem<IVariable>
IProblem<IVariable> genericProblem = problem;
}
}
为什么我需要将变量genericProblem明确地转换为
@SuppressWarnings("unchecked")
IProblem<IVariable> genericProblem = (IProblem<IVariable>) problem;
并收到警告?
该方法将P作为参数,其类型为IProblem,因为V也必须实现IVariable。 我的错误在哪里?解决方法是什么?
我不想写
IProblem<V> genericProblem = problem;
因为问题的输入变量可能不同。
无论如何,我认为IProblem<V>
比IProblem<IVariable>
更具体。
提前致谢!
答案 0 :(得分:2)
即使IProblem<V>
被约束为IProblem<IVariable>
,V
也不等同于IVariable
,因为Java的泛型是不变的。当然,IProblem<V>
是要走的路,但如果你不想这样,你可以使用上限通配符代表V
和IVariable
之间的关系:
IProblem<? extends IVariable> genericProblem = problem;
答案 1 :(得分:1)
泛型类型需要完全匹配。 V extends IVariable
和IVariable
不是可互换的类型。让我再举一个例子。
List<Integer> ints = new ArrayList<>(); // list of ints.
List<Number> nums = ints; // doesn't compile but this is what you are trying to do
nums.add(new BigDecimal(0.0)); // fine as BigDecimal is a Number.