Java中关于泛型擦除的小结


java由于擦除,在使用泛型的时候一些操作成了程序存在异常的可能 ,这个类中展示了一些容易出现的问题,下面的小例子做了一个简单的总结

 
  1. class Erased<T> {  
  2.   
  3.     public static void f(Object arg) {  
  4.         // if (arg instanceof T) {   
  5.         // } // Error   
  6. //       T var = new T(); // Error   
  7. //       T[] array = new T[SIZE]; // Error   
  8. //       T[] array = (T) new Object[SIZE]; // Unchecked warning   
  9.     }  
  10. }  

下面的程序算是对擦除特性的一个弥补

 
  1. /*casionally you can program around these issues, but sometimes you must compensate for erasure by introducing a type 
  2.  * tag . This means you explicitly pass in the Class object for your type so that you can use it in type expressions. 
  3.  * For example, the attempt to use instanceof in the previous program fails because the type information has been 
  4.  * erased. If you introduce a type tag, a dynamic islnstance( ) can be used instead: 
  5. */  
  6.   
  7. public class CompensatErasure<T> {  
  8.     public static void main(String[] args) {  
  9.         CompensatErasure<Building> ctt1 = new CompensatErasure<Building>(Building.class);  
  10.         ctt1.f(new Building());  
  11.         ctt1.f(new House());  
  12.         CompensatErasure<House> ctt2 = new CompensatErasure<House>(House.class);  
  13.         ctt2.f(new Building());  
  14.         ctt2.f(new House());  
  15.     }  
  16.   
  17.     private Class<T> type;  
  18.   
  19.     public CompensatErasure(Class<T> type) {  
  20.         this.type = type;  
  21.     }  
  22.   
  23.     /** 
  24.      * @return the type 
  25.      */  
  26.     public Class<T> getType() {  
  27.         return type;  
  28.     }  
  29.   
  30.     /** 
  31.      * @param type 
  32.      *            the type to set 
  33.      */  
  34.     public void setType(Class<T> type) {  
  35.         this.type = type;  
  36.     }  
  37.   
  38.     @SuppressWarnings("unchecked")  
  39.     public void f(Object obj) {  
  40.         // Compensate instance of   
  41.         if (type.isInstance(obj)) {  
  42.             System.out.println(obj + " is instance of " + type.getSimpleName());  
  43.         } else {  
  44.             System.out.println(obj + " doesn't  instance of " + type.getSimpleName());  
  45.         }  
  46.         try {  
  47.             // Compensate new T();   
  48.             System.out.println(type.newInstance());  
  49.             // compensate new T[]{};   
  50.             Class<T>[] types = new Class[1];  
  51.         } catch (InstantiationException e) {  
  52.             e.printStackTrace();  
  53.         } catch (IllegalAccessException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.   
  58. }  
  59.   
  60. class Building {  
  61.     @Override  
  62.     public String toString(){  
  63.         return "Building";  
  64.     }  
  65. }  
  66.   
  67. class House extends Building {  
  68.     @Override  
  69.     public String toString(){  
  70.         return "House";  
  71.     }  
  72. }  

相关内容