因此,我正在阅读泛型以重新熟悉这些概念,特别是在涉及通配符的地方,因为我很少使用它们或碰到它们。从我所做的阅读中,我无法理解他们为什么使用通配符。我不断遇到的一个例子如下。Java泛型:通配符
void printCollection(Collection<?> c) {
for (Object o : c){
System.out.println(o);
}
}
为什么你会不会写为:
<T> void printCollection(Collection<T> c) {
for(T o : c) {
System.out.println(o);
}
}
从Oracle网站又如:
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
这是为什么不写成
public static <T extends Number> double sumOfList(List<T> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}
上午我错过了什么?
可能[duplicate](http://stackoverflow.com/questions/10943137/difference-between-generic-type-and-wildcard-type) – Jayamohan 2013-02-28 01:08:22
@Jayamohan我不同意。 – 2013-02-28 01:10:04