1介绍
1 List myIntList=new LinkedList(); 3 myIntList.add(newInteger(0)); 5 Integer x=(Integer)myIntList.iterator().next();
1 List<Integer> myIntList=newLinkedList<Integer>; 3 myIntList.add(newInteger(0)); 5 Integer x=myIntList.iterator().next();
1 public interface List<E>{ 3 void add(E,x); 5 Iterator<E> iterator(); 7 } 9 public interface Iterator<E>{ 11 E next(); 13 boolean hasNext(); 15 }
1 public interface List<Integer>{ 2 void add(Integer x); 3 terator<Integer> iterator(); 4 }
1 /*会导致编译时出错*/ 2 public class Erasure{ 3 public void test(List<String> ls){ 4 System.out.println("String"); 5 } 6 public void test(List<Integer> ls){ 7 System.out.println("Integer"); 8 } 9 }
1 List<Apple> apples=new ArrayList<Apple>(); 2 List<Fruit> fruits=apples;
1 //不使用泛型 2 void printCollection(Collection c){ 3 Iterator i=c.iterator(); 4 for(k=0;k<c.size();k++){ 5 System.out.println(i.next()); 6 } 7 }
1 //使用泛型 2 void printCollection(Collection c)<Object> c{ 3 for(Object e:c){ 4 System.out.println(e); 5 } 6 }
1 void printCollection(Collection c)<?> c{ 2 for(Object e:c){ 3 System.out.println(e); 4 } 5 }
1 Collection<?> c=new ArrayList<String>(); 2 c.add(newObject());//编译出错,不管加入什么对象都出错,除了null外。 3 c.add(null);//OK!
1 public abstract class Shape{ 2 public abstract void draw(Canvas c); 3 } 4 public class Circle extends Shape{ 5 private int x,y; 6 public void draw(Canvas c){...}; 7 } 8 public class Rectangle extends Shape{ 9 private int x,y,width,height; 10 public void draw(Canvas c){...} 11 } 12 13 //原始版本 14 public void drawAll(List<Shape> shapes){ 15 for(Shapes :shapes){ 16 s.draw(this); 17 } 18 } 19 20 //使用边界通配符的版本 21 public void drawALL(List<? extends Shape> shapes){ 22 for(Shapes :shapes){ 23 s.draw(this); 24 } 25 }
1 List<Shape> shapes=new ArrayList<Shape>(); 2 List<? super Cicle> cicleSupers=shapes; 3 circleSupers.add(new Cicle());//OK,subclss of Cicle also OK 4 cicleSupers.add(new Shape());//ERROR
<!--[if !supportLists]-->l <!--[endif]-->如果你想从一个数据类型里获取数据,使用 ? extends 通配符
<!--[if !supportLists]-->l <!--[endif]-->如果你想把对象写入一个数据结构里,使用 ? super 通配符
<!--[if !supportLists]-->l <!--[endif]-->如果你既想存,又想取,那就别用通配符。
5 泛型方法
1 static void fromArrayToCollection(Object[]a,Collection<?>c){ 2 for(Object o:a){ 3 c.add(o);//编译错误 4 } 5 }
1 static<T> void fromArrayToCollection(T[] a,Collection<T>c){ 2 for(T O:a){ 3 c.add(o);//OK 4 } 5 }
1 Object[] oa=new Object[100]; 2 Collection<Object>co=new ArrayList<Object>(); 3 fromArrayToCollection(oa,co);//T inferred to be Object 4 String[] sa=new String[100]; 5 Collection<String>cs=new ArrayList<String>(); 6 fromArrayToCollection(sa,cs);//T inferred to be String 7 fromArrayToCollection(sa,co);//T inferred to be Object 8 Integer[] ia=new Integer[100]; 9 Float[] fa=new Float[100]; 10 Number[] na=new Number[100]; 11 Collection<Number>cn=new ArrayList<Number>(); 12 fromArrayToCollection(ia,cn);//T inferred to be Number 13 fromArrayToCollection(fa,cn);//T inferred to be Number 14 fromArrayToCollection(na,cn);//T inferred to be Number 15 fromArrayToCollection(na,co);//T inferred to be Object 16 fromArrayToCollection(na,cs);//编译错误
1 public <T> void go(T t){ 2 System.out.println("generic function"); 3 } 4 public void go(String str){ 5 System.out.println("normal function"); 6 } 7 public static void main(String[] args){ 8 FuncGenric fg=new FuncGenric(); 9 fg.go("haha");//打印normal function 10 fg.<String>go("haha");//打印normal function,String是多余的,并且不报错。 11 fg.go(new Object());//打印generic function 12 fg.<Object>go(new Object());//打印generic function 13 }
1 //代码一:编译出错 2 public class Erasure{ 3 public void test(int i){ 4 System.out.printlnl("String"); 5 } 6 public void test(int i){ 7 System.out.println("Integer"); 8 } 9 }
1 //代码二:正确 2 public class Erasure{ 3 public void test(List<String>ls){ 4 System.out.println("String"); 5 } 6 public void test(List<Integer>li){ 7 System.out.println("Integer"); 8 } 9 }
1 List<Sting>l1=mew ArrayList<String>(); 2 List<Integer>l2=mew ArrayList<Integer>(); 3 System.out.println(l1.getClass()==l2.getClass());//True
3)instanceof
1 Collection cs=new CollectionList<String>(); 2 if (cs instanceof Collection<String>){....}//编译出错,如果改成instanceof Collection<?>则正确。
1 List<String>[] lsa=new ArrayList<String>[10];//错误。因为如果可以这样,那么考虑如下代码,会导致运行时错误。 2 List<String>[] lsa=new ArrayList<String>[10];//实际上并不允许这样创建数组 3 Object 0=lsa; 4 Object[] oa=(Object[])o; 5 List<Integer>li=new ArrayList<Integer>(); 6 li.add(new Integer(3)); 7 oa[1]=li;//unsound,but passes run time store check 8 String s=lsa[1].get[0];//run-time error-classCastException
1 List<?>[] lsa=new List<?>[10]; 2 Object 0=lsa; 3 Object[] oa=(Object[])o; 4 List<Integer>li=new ArrayList<Integer>(); 5 li.add(new Integer(3)); 6 oa[1]=li;/OK 7 String s=lsa[1].get(0);//run-time error,but cast is explicit 8 Integer it=(Integer)lsa[1].get(0);//OK
原文:http://www.cnblogs.com/liuzhongfeng/p/5080989.html