java泛型与object的比较

在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。

泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动隐式的,提高代码的重用率。
 
  1. public class Gen<T> {  
  2.     private T ob; // 定义泛型成员变量  
  3.   
  4.     public Gen(T ob) {  
  5.         this.ob = ob;  
  6.     }  
  7.   
  8.     public T getOb() {  
  9.         return ob;  
  10.     }  
  11.   
  12.     public void setOb(T ob) {  
  13.         this.ob = ob;  
  14.     }  
  15.   
  16.     public void showType() {  
  17.         System.out.println("T的实际类型是: " + ob.getClass().getName());  
  18.     }  
  19. }  
public 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());
	}
}
  1. public class GenDemo {  
  2.     public static void main(String[] args) {  
  3.         // 定义泛型类Gen的一个Integer版本  
  4.         Gen<Integer> intOb = new Gen<Integer>(88);  
  5.         intOb.showType();  
  6.         int i = intOb.getOb();  
  7.         System.out.println("value= " + i);  
  8.         System.out.println("----------------------------------");  
  9.         // 定义泛型类Gen的一个String版本  
  10.         Gen<String> strOb = new Gen<String>("Hello Gen!");  
  11.         strOb.showType();  
  12.         String s = strOb.getOb();  
  13.         System.out.println("value= " + s);  
  14.     }  
  15. }  
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 Gen2 {  
  2.     private Object ob; //定义一个通用类型成员  
  3.     public Gen2(Object ob) {  
  4.     this.ob = ob;  
  5.     }  
  6.     public Object getOb() {  
  7.     return ob;  
  8.     }  
  9.     public void setOb(Object ob) {  
  10.     this.ob = ob;  
  11.     }  
  12.     public void showTyep() {  
  13.     System.out.println("T的实际类型是: " + ob.getClass().getName());  
  14.     }  
  15. }  
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());
	}
}
  1. public class GenDemo2 {  
  2.     public static void main(String[] args) {  
  3.         //定义类Gen2的一个Integer版本  
  4.         Gen2 intOb = new Gen2(new Integer(88));  
  5.         intOb.showTyep();  
  6.         int i = (Integer) intOb.getOb();  
  7.         System.out.println("value= " + i);  
  8.         System.out.println("---------------------------------");  
  9.         //定义类Gen2的一个String版本  
  10.         Gen2 strOb = new Gen2("Hello Gen!");  
  11.         strOb.showTyep();  
  12.         String s = (String) strOb.getOb();  
  13.         System.out.println("value= " + s);  
  14.         }  
  15. }  
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);
		}
}

posted on 2017-04-15 11:49  范景锋  阅读(3777)  评论(0编辑  收藏  举报

导航