泛型:实现了参数化类型,使代码可以应用于多种类型。
泛型的引出:
先看以下代码:
public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("aa"); al.add("bbb"); al.add("cccc"); al.add(1); for(Iterator it = al.iterator(); it.hasNext();){ Object obj = it.next(); String str = (String)obj; System.out.println(str.length()); } }
代码编译通过,但是在运行后报出以下异常:
产生该异常的原因,在List中即添加了字符串又添加了Integer,而从List中取出的对象都是object,当对对象应用持有的方法的时候,需要向下转行,只要使用向下转型就有可能发生异常。
使用泛型对集合中持有的对象进行限定:
public static void main(String[] args) { /** * 泛型 */ ArrayList<String> als = new ArrayList<String>(); als.add("aa"); als.add("bbb"); als.add("cccc"); Iterator<String> its = als.iterator(); while(its.hasNext()){ String next = its.next(); System.out.println(next); } }
泛型的好处:
class Demo<Q> { public void run(Q q){ System.out.println(q); } }类上泛型的应用:
class Demo2 { int i; public int getI() { return i; } public void setI(int i) { this.i = i; } } class Demo3 { String s; public String getS() { return s; } public void setS(String s) { this.s = s; } } class Demo4 { Object obj; public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } }
public class Test{ <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public static void main(String[] args) { <span style="white-space:pre"> </span>Demo<Integer> demo = new Demo<Integer>(); <span style="white-space:pre"> </span>demo.run(11); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>Demo4 demo4 = new Demo4(); <span style="white-space:pre"> </span>demo4.setObj(new Run()); <span style="white-space:pre"> </span>Object str1 = demo4.getObj(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> }可以看出,即使使用Object来代替具体属性,如果需要对对象应用其持有的方法,还是需要向下转型,还是可能抛出异常。
class Demo5<T> { T t; public T getT() { return t; } public void setT(T t) { this.t = t; } } public class Test{ public static void main(String[] args) { Demo5<Integer> demo5 = new Demo5<Integer>(); demo5.setT(22); System.out.println(demo5.getT()); Demo5<String> demo55 = new Demo5<String>(); demo55.setT("aaaa"); System.out.println(demo55.getT()); Demo5<Run> demo555 = new Demo5<Run>(); demo555.setT(new Run()); demo555.getT().start(); } }
public class Run { public void start(){ System.out.println("-----start------"); } }
public class Test { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("aa"); al.add("bb"); al.add("cc"); printCollection(al); ArrayList<Integer> al2 = new ArrayList<Integer>(); al2.add(22); al2.add(24); al2.add(25); printCollection(al2); LinkedList<String> ll = new LinkedList<String>(); ll.add("Java"); ll.add("C++"); ll.add("Python"); printCollection(ll); } public static void printCollection(Collection<?> list){ for(Iterator<?> it = list.iterator(); it.hasNext();) { System.out.println(it.next()); } System.out.println("------------------"); } }输出:
public class Test2 { public static void main(String[] args) { ArrayList<Person> al = new ArrayList<Person>(); al.add(new Person("zhangsan", 22 , "nan")); al.add(new Person("lisi", 23, "nv")); al.add(new Person("wangwu", 25, "nan")); printCollection(al); LinkedList<Student> ll = new LinkedList<Student>(); ll.add(new Student("张三明", 23, "男")); ll.add(new Student("李小白", 34, "男")); printCollection(ll); TreeSet<String> ts = new TreeSet<String>(); ts.add("aa"); ts.add("bb"); ts.add("cc"); //printCollection(ts); } public static void printCollection(Collection<? extends Person> c) { for(Iterator<? extends Person> it = c.iterator(); it.hasNext();) { System.out.println(it.next()); } System.out.println("----------------------------------"); } }
public class Person { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public Person(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } public Person() { } }
public class Student extends Person{ public Student() { super(); } public Student(String name, int age, String sex) { super(name, age, sex); }; }输出:
interface Inter<W> { public void run(W w); } class InterImpl implements Inter<String> { @Override public void run(String w) { System.out.println(w); } } class InterImpl2<T> implements Inter<T> { @Override public void run(T t) { System.out.println(t); } } public class Test3 { public static void main(String[] args) { InterImpl ii = new InterImpl(); ii.run("aaaa"); InterImpl2<String> ii2 = new InterImpl2<String>(); ii2.run("bbbbb"); } }<strong> </strong>输出: