2013-04-25 70 views
1

我想創建一個方法,可以返回不同類型的數據的列表。 type參數用於標識需要處理哪種類型的數據並返回getListedData(Class類型)。我還寫了一個私有方法createList(類類型,列表列表,字符串標誌),可以被所有類型的數據用來創建列表。方法返回與參數化類型作爲參數的不同類型的對象列表

該代碼正在做我期待的,但作爲新的泛型,我不覺得這是寫作的最佳方式。任何人都可以給我一些建議嗎?特別是使用LIst和反射構造函數創建對象實例時使用泛型。 (我使用它的原因是使該方法可以重用於所有類型)。而我通過澆鑄:(

這些不同類型的類具有相同的結構感到惱火。

public class TypeA { 
     private String propOne; 
     public TypeA(String propOne) { 
      super(); 
      this.propOne = propOne; 
     } 
     public String getPropOne() { 
      return propOne; 
     } 
     public void setPropOne(String propOne) { 
      this.propOne = propOne; 
     }   
    } 

    public class TypeB { 
     private String propOne; 
     public TypeB(String propOne) { 
      super(); 
      this.propOne = propOne; 
     } 
     public String getPropOne() { 
      return propOne; 
     } 
     public void setPropOne(String propOne) { 
      this.propOne = propOne; 
     } 
    } 

將有相同的結構化數據類型的負載。

public class Test { 
     @SuppressWarnings("unchecked") 
     public static void main(String arg[]) throws SecurityException, IllegalArgumentException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException{ 
      Test test = new Test(); 
      List<TypeB> b = (List<TypeB>) test.getListedData(TypeB.class); 
      List<TypeA> a = (List<TypeA>) test.getListedData(TypeA.class); 
          //similar repeate..... 
     } 

     public <T> List<T> getListedData(Class<T> type) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{ 
      List<T> list = new ArrayList<T>(); 
      String flag = ""; 
      if(type.equals(TypeA.class)){ 
       flag = "A"; 
       createList(type, list, flag); 
      }else{ 
       flag = "B"; 
       createList(type, list, flag); 
      } 
      return list; 
     } 

     private <T> void createList(Class<T> type, List<T> list, String flag) 
       throws NoSuchMethodException, InstantiationException, 
       IllegalAccessException, InvocationTargetException { 
      for(int i=0; i<2; i++){ 
       Constructor<?> ctor = type.getConstructor(String.class); 
       Object object = ctor.newInstance(new Object[] {String.valueOf(i)}); 
       list.add((T) object); 
       //do something with flag... 
      } 
     } 
    } 

任何建議將是謝謝

+1

我會'公共列表 getListedData(類型)',因爲它消除了鑄造的返回值的需要。 – 2013-04-25 16:11:09

+0

instanceOf方法可能是您的答案: – 2013-04-25 16:12:45

+0

您也可以編寫'ctor.newInstance(「」+ i);' – 2013-04-25 16:13:23

回答

0

更好的避免個案 你可以這樣做,或者通過反射也可以使用類的靜態成員

public <T> List<T> getListedData(Class<T> type) throws SecurityException, 
     NoSuchMethodException, IllegalArgumentException, InstantiationException, 
     IllegalAccessException, InvocationTargetException { 
    List<T> list = new ArrayList<>(); 
    String flag = type.getSimpleName(); 
    createList(type, list, flag); 
    return list; 
} 

private <T> void createList(Class<T> type, List<T> list, String flag) 
     throws NoSuchMethodException, InstantiationException, 
     IllegalAccessException, InvocationTargetException { 
    for (int i = 0; i < 2; i++) { 
     Constructor<?> ctor = type.getConstructor(String.class); 
     Object object = ctor.newInstance(new Object[]{String.valueOf(i)}); 
     list.add((T) object); 
     //do something with flag... 
    } 
} 
相關問題