有人可以解释为什么以下代码片段无法编译吗?
public class Main {
public static void main(String[] args) {
Integer[] integers = {3, 5, 8};
Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(integers));
Double[] doubles = {3.5, 5.5, 8.5};
Set<Double> s2 = new HashSet<Double>(Arrays.asList(doubles));
Set<Number> res1 = union(s1, s1); // ->it does not compile
Set<Number> res2 = union(s1, s2); // ->it does not compile
}
static <E> Set<E> union(Set<? extends E> s1, Set<? extends E> s2)
{
Set<E> result = new HashSet<E>(s1);
result.addAll(s2);
return result;
}
}
我已阅读错误消息:
Type mismatch: cannot convert from Set<Integer> to Set<Number> Main.java /TestingGenerics/src/com/al/testinggenerics line 17 Java Problem
Type mismatch: cannot convert from Set<Number&Comparable<?>> to Set<Number> Main.java /TestingGenerics/src/com/al/testinggenerics line 18 Java Problem
只要
Integer
和Double
从Number
扩展,是什么导致问题? 最佳答案
如user3580294(+1)和my comment中所述:原因是Java 7类型推断不够智能。
但是,您可以通过省略通配符,而仍然保持所需的灵活性,从而胜过类型推断:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TargetTypeInferenceTest
{
public static void main(String[] args)
{
Integer[] integers = { 3, 5, 8 };
Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(integers));
Double[] doubles = { 3.5, 5.5, 8.5 };
Set<Double> s2 = new HashSet<Double>(Arrays.asList(doubles));
Set<Number> res1 = union(s1, s1); // ->it does compile ;-)
Set<Number> res2 = union(s1, s2); // ->it does compile ;-)
}
static <E, F extends E, G extends E> Set<E> union(Set<F> s1, Set<G> s2)
{
Set<E> result = new HashSet<E>(s1);
result.addAll(s2);
return result;
}
}