在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。
泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,提高代码的重用率
无泛型:
package com.yuan;
public class Gen2 {
private Object ob; //定义一个通用类型成员
public Gen2(Object ob) {
this.ob = ob;
}
public Object getOb() {
return ob;
}
public void setOb(Object ob) {
this.ob = ob;
}
public void showTyep() {
System.out.println("T的实际类型是: " + ob.getClass().getName());
}
}
测试类:
package com.yuan;
public class GenDemo2 {
public static void main(String[] args) {
//定义类Gen2的一个Integer版本
Gen2 intOb = new Gen2(new Integer(88));
intOb.showTyep();
int i = (Integer) intOb.getOb();
System.out.println("value= " + i);
System.out.println("---------------------------------");
//定义类Gen2的一个String版本
Gen2 strOb = new Gen2("Hello Gen!");
strOb.showTyep();
String s = (String) strOb.getOb();
System.out.println("value= " + s);
}
}
getOb的方法调用 需要强制类型转换。
有泛型:
package com.yuan;
class Gen<T> {
private T ob; //定义泛型成员变量
public Gen(T ob) {
this.ob = ob;
}
public T getOb() {
return ob;
}
public void setOb(T ob) {
this.ob = ob;
}
public void showType() {
System.out.println("T的实际类型是: " + ob.getClass().getName());
}
}
测试类:
package com.yuan;
public class GenDemo {
public static void main(String[] args) {
//定义泛型类Gen的一个Integer版本
Gen<Integer> intOb = new Gen<Integer>(88);
intOb.showType();
int i = intOb.getOb();
System.out.println("value= " + i);
System.out.println("----------------------------------");
//定义泛型类Gen的一个String版本
Gen<String> strOb = new Gen<String>("Hello Gen!");
strOb.showType();
String s = strOb.getOb();
System.out.println("value= " + s);
}
}
在我认为,就是调用方决定类型,被调用不确定类型。对于抽离出一个统一的功能类或者方法有好处。
1、限制泛型
public class CollectionGenFoo<T extends Collection> { private T x; public CollectionGenFoo(T x) { this.x = x; } public T getX() { return x; } public void setX(T x) { this.x = x; } }
测试:
public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null; listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); //出错了,不让这么干。 // CollectionGenFoo<Collection> listFoo = null; // listFoo=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); } }
2、通配符泛型
解决限制性泛型问题
public class CollectionGenFoo<? extends Collection> { private T x; public CollectionGenFoo(T x) { this.x = x; } public T getX() { return x; } public void setX(T x) { this.x = x; } }
测试:
public class CollectionGenFooDemo { public static void main(String args[]) { CollectionGenFoo<ArrayList> listFoo = null; listFoo = new CollectionGenFoo<ArrayList>(new ArrayList()); //现在不会出错了 CollectionGenFoo<? extends Collection> listFoo1 = null; listFoo=new CollectionGenFoo<ArrayList>(new ArrayList()); System.out.println("实例化成功!"); } }
总结:
1、如果只指定了<?>,而没有extends,则默认是允许Object及其下的任何Java类了。也就是任意类。
2、通配符泛型不单可以向下限制,如<? extends Collection>,还可以向上限制,如<? super Double>,表示类型只能接受Double及其上层父类类型,如Number、Object类型的实例。
3、泛型类定义可以有多个泛型参数,中间用逗号隔开,还可以定义泛型接口,泛型方法。这些都与泛型类中泛型的使用规则类似。
3、泛型方法
public class ExampleA { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { ExampleA ea = new ExampleA(); ea.f(" "); ea.f(10); ea.f('a'); ea.f(ea); } }
输出结果:
java.lang.String
java.lang.Integer
java.lang.Character
ExampleA
使用泛型方法时,不必指明参数类型,编译器会自己找出具体的类型。泛型方法除了定义不同,调用就像普通方法一样。
需要注意,一个static方法,无法访问泛型类的类型参数,所以,若要static方法需要使用泛型能力,必须使其成为泛型方法。