(1)集合中使用类型 List示例: List l = new ArrayList(); 这时,List加入了泛型。在add元素进去时候,只能使用String类型。同样,在get()的时候自动就会提取出String。
Set示例: Collection c = new HashSet(); 同样,add只能使用String类型。在使用Iterator迭代时,可以使用如下方式, for(Iterator i = c.iterator();it.hasNext();){ String s = it.next(); } Map示例: Map m = new HashMap();
(2)自定义的泛型类 public class Person{ private T secrecy; public void setSecrecy(T s){ this.secrecy = s; } } 这样在使用的时候就可以 Person p = new Person();