泛型
泛型
<>發(fā)音為typeof ArrayList<Integer> ArrayList typeof Integer
ArrayList成為原始類型
1.泛型是給編譯器看的,為了規(guī)范數(shù)據(jù)類型。以前的集合可以添加各種類型的對象。
2.通過字節(jié)碼比較 collection3.getClass() == collection2.getClass()
可以發(fā)現(xiàn),編譯過后的字節(jié)碼中并沒有類型信息。
3.可以通過反射,透過泛型往集合中添加各種類型元素
collection3.getClass().getMethod("add",Object.class).invoke(collection3,"abc");
4.參數(shù)化類型不考慮類型參數(shù)的繼承
Vector<String> v= new Vector<Object>() //錯誤
Vector<Object> v= new Vector<String>() //錯誤
5.在創(chuàng)建數(shù)組實例時,數(shù)組的元素不能使用參數(shù)化的類型
Vector<Integer> vectorList[] = new Vector<Integer>[10];
? 通配符。 用于通配任意類型的集合
public static void printCollection(Collection<?> collection){
collection.size();
//collection.add(); 可以調(diào)用與參數(shù)化無關(guān)的方法
}
posted on 2011-01-04 10:05 杰點 閱讀(145) 評論(0) 編輯 收藏 所屬分類: JAVA