因此,我正在閱讀泛型以重新熟悉這些概念,特別是在涉及通配符的地方,因爲我很少使用它們或碰到它們。從我所做的閱讀中,我無法理解他們爲什麼使用通配符。我不斷遇到的一個例子如下。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