什么是泛型
集合框架中的泛型
用法:
创建集合容器时规定其允许保存的元素类型,然后由编译器负责添加元素的类型合法性检查,在取用集合元素时则不必再进行造型处理.
使用泛型的列表集合
import java.util.Vector; import java.util.Date; public class TestVector{ public static void main(String args[]){ Vector<String> v = new Vector<String>(); v.addElement("Tom"); v.addElement("Billy"); v.addElement("Nancy"); v.addElement(new Date()); v.addElement(new Integer(300)); for(int i=0;i<v.size();i++){ String name = v.elementAt(i); System.out.println(name); } } }
在Hashtable中使用泛型
public class Employee{ private final int id; private String name; private double salary; public Employee(int id,String name,double salary){ this.id = id; this.name = name; this.salary = salary; } public int getId(){ return id; } public String geName(){ return name; } public void setName(String name){ this.name = name; } public double getSalary(){ return salary; } public void setSalary(double salary){ this.salary = salary; } public void showInfo(){ System.out.println(id + "\t" + name + "\t" + salary); } }
import java.util.Hashtable; public class TestHashtable{ public static void main(String args[]){ Hashtable<Integer,Employee> ht = new Hashtable<Integer,Employee>(); ht.put(101,new Employee(101,"张三",5000)); ht.put(102,new Employee(102,"李四",4800)); ht.put(106,new Employee(106,"赵六",8620)); Employee e = ht.get(102); e.showInfo(); } }
泛型的向后兼容性
java语言中的泛型向过去兼容
在高版本开发环境中编译未启用泛型机制的集合框架应用代码时,会输出类似于如下形式的编译提示信息:
注意:C:\text\test.java 使用了未经检查或不安全的操作 注意:要了解详细信息,请使用-Xlint:unchecked重新编译
可以使用SuppressWarnings注解,关闭编译提示或警告信息
关闭编译提示
import java.util.Date; import java.util.Vector; import java.lang.SuppressWarnings; @SuppressWarnings({"unchecked"}) public class TestSuppressWarnings{ public static void main(String args[]){ Vector v = new Vector(); v.addElement("Tom"); v.addElement("Billy"); v.addElement("Nancy"); v.addElement(new Date()); v.addElement(new Integer(300)); for(int i=0;i<v.size();i++){ Object o = v.elementAt(i); System.out.println(o); } } }
泛型类:
泛型类举例:
public class vector<E>{ public void addElement(E obj){---} public E elementAt(int index){---} }
使用泛型类时,应明确指定类型参数
Vector<String> v=new Vector<String>();
public class Person<T>{ private final int id; private T secrecy; public Person(int id){ this.id = id; } public int getId(){ return id; } public void setSsecrecy(T secrecy){ this.secrecy = secrecy; } public T getSecrecy(){ return secrecy; } }
public class Test{ public static void main(String[] args){ Person<String> p1 = new Person<String>(101); p1.setSsecrecy("芝麻开门"); String s = p1.getSecrecy(); System.out.println(p1.getId() + "\t密码是:" + s); Person<Double> p2 = new Person<Double>(102); p2.setSsecrecy(8700.45); double money = p2.getSecrecy(); System.out.println(p2.getId() + "\t秘密资金数额:" + money); } }
同一个泛型类搭配不同的类型参数复合而成的类型属于同一个类,但却是不同的类型例如:
Vector<String>与Vector<Double>
同一个泛型类与不同的额类型参数复合而成的类型间并不存在继承关系,即使是类型参数间存在继承关系时也是如此例如
Vector<String>与Vector<Object>
类型通配符"?"
import java.util.Vector; public class Test{ public static void main(String[] args){ Test t = new Test(); Vector<String> vs = new Vector<String>(); vs.add("Tom"); vs.add("Billy"); vs.add("Kessey"); t.overview(vs); Vector<Integer> vi = new Vector<Integer>(); vi.add(300); vi.add(500); t.overview(vi); } public void overview(Vector<?> v){ for(Object o:v){ System.out.println(o); } } }
使用泛型方法
import java.util.Vector; public class Test{ public static void main(String[] args){ Test t = new Test(); String valid = t.evaluate("tiger","tiger"); Integer i = t.evaluate(new Integer(300),new Integer(350)); System.out.println(valid); System.out.println(i); } public <T> T evaluate(T a,T b){ if(a.equals(b)) return a; else return null; } }
受限制的类型参数
泛型机制允许开发者对类型参数进行附加约束.
import java.lang.Number; public class Point<T extends Number>{ private T x; private T y; public Point(){ } public Point(T x, T y){ this.x = x; this.y = y; } public T getX(){ return x; } public T getY(){ return y; } public void setX(T x){ this.x = x; } public void setY(T y){ this.y = y; } public void showInfo(){ System.out.println("x=" + x + ",y=" + y); } }
public class Test{ public static void main(String[] args){ Point<Integer> pi = new Point<Integer>(20,40); pi.setX(pi.getX() + 100); pi.showInfo(); Point<Double> pd = new Point<Double>(); pd.setX(3.45); pd.setY(6.78); pd.showInfo(); Point<String> ps = new Point<String>(); } }
受限制的类型通配符
import java.util.Vector; public class Test{ public static void main(String[] args){ Test t = new Test(); Vector<Integer> vi = new Vector<Integer>(); vi.add(300); vi.add(500); t.show(vi); Vector<Double> vd = new Vector<Double>(); vd.add(3.14); vd.add(5.78); t.show(vd); Vector<String> vs = new Vector<String>(); vs.add("Tom"); vs.add("Billy"); //t.show(vs); Vector<? extends Number> va ; va = vi; va = vd; //va = vs; } public void show(Vector<? extends Number> v){ System.out.println(v); } }