Java针对泛型创建对象的中存在擦除的弥补方案


在java中不能像才c++那样,直接声明泛型对象即使 T t=new T(); 但是java中针对这种问题也有一些解决方案,在这里提供三种方案

 
  1. /** 
  2.  *  
  3.  * although in c++ could use new T() to creating a instance of type, but java not support this approach, this class 
  4.  * provider 3 solutions for this situation 
  5.  * @version $Revision: $ $Name: $ 
  6.  */  
  7. public class CreatingTypesSolutions {  
  8.     public static void main(String[] args) {  
  9.         // solution 1: pass in a factory object, and use that to make the new instance   
  10.         ClassAsFactory<Employ> caf1 = new ClassAsFactory<Employ>(Employ.class);  
  11.         /* 
  12.          * This compiles, but fails with ClassAsFactory<Integer> because Integer has no default constructor. Because the 
  13.          * error is not caught at compile time, this approach is frowned upon by the Sun folks. They suggest instead 
  14.          * that you use solution2 
  15.          */  
  16.         // ClassAsFactory<Integer> caf2 = new ClassAsFactory<Integer>(Integer.class);   
  17.         // solution 2:   
  18.         ClassAsFactory2<Employ> caf21 = new ClassAsFactory2<Employ>(new EmployFactory());  
  19.         ClassAsFactory2<Integer> caf22 = new ClassAsFactory2<Integer>(new IntegerFactory());  
  20.         // solution 3: Template Method design pattern   
  21.         IntegerGeneric ig1=new IntegerGeneric();  
  22.         System.out.println(ig1.create());  
  23.     }  
  24.   
  25. }  
  26.   
  27. class Employ {  
  28. }  
  29.   
  30. class ClassAsFactory<T> {  
  31.     public ClassAsFactory(Class<T> t) {  
  32.         try {  
  33.             T tObj = t.newInstance();  
  34.             System.out.println(tObj.getClass().getSimpleName() + " object!");  
  35.         } catch (InstantiationException e) {  
  36.             e.printStackTrace();  
  37.         } catch (IllegalAccessException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42. }  
  43.   
  44. // solution 2 :intgerface   
  45. interface FactoryI<T> {  
  46.     T create();  
  47. }  
  48.   
  49. class ClassAsFactory2<T> {  
  50.     public <F extends FactoryI<T>> ClassAsFactory2(F factory) {  
  51.         System.out.println(factory.create().getClass().getSimpleName() + " create successful");  
  52.     }  
  53. }  
  54.   
  55. class EmployFactory implements FactoryI<Employ> {  
  56.   
  57.     public Employ create() {  
  58.         return new Employ();  
  59.     }  
  60.   
  61. }  
  62.   
  63. class IntegerFactory implements FactoryI<Integer> {  
  64.   
  65.     public Integer create() {  
  66.         return new Integer(0);  
  67.     }  
  68.   
  69. }  
  70.   
  71. // solution 3: Template Method design pattern   
  72. abstract class GenericWithCreate<T> {  
  73.     final T element;  
  74.   
  75.     GenericWithCreate() {  
  76.         element = create();  
  77.     }  
  78.   
  79.     abstract T create();  
  80. }  
  81.   
  82. class IntegerGeneric extends GenericWithCreate<Integer> {  
  83.      
  84.     @Override  
  85.     Integer create() {  
  86.         return new Integer("4");  
  87.     }  
  88.       
  89.   
  90. }  

相关内容