杰点

hello java

泛型

泛型 

<>发音为typeof  ArrayList<Integer> ArrayList typeof Integer 

ArrayList成为原始类型

1.泛型是给编译器看的,为了规范数据类型。以前的集合可以添加各种类型的对象。

2.通过字节码比较 collection3.getClass() == collection2.getClass()
可以发现,编译过后的字节码中并没有类型信息。

3.可以通过反射,透过泛型往集合中添加各种类型元素
 collection3.getClass().getMethod("add",Object.class).invoke(collection3,"abc");

4.参数化类型不考虑类型参数的继承
Vector<String> v= new Vector<Object>() //错误
Vector<Object> v= new Vector<String>() //错误

5.在创建数组实例时,数组的元素不能使用参数化的类型
Vector<Integer> vectorList[] = new Vector<Integer>[10];


? 通配符。 用于通配任意类型的集合 
 public static void printCollection(Collection<?> collection){
 collection.size();
 //collection.add(); 可以调用与参数化无关的方法
}

posted on 2011-01-04 10:05 杰点 阅读(147) 评论(0)  编辑  收藏 所属分类: JAVA


只有注册用户登录后才能发表评论。


网站导航:
 
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

留言簿

文章分类

文章档案

搜索

最新评论