2016-03-17 23 views
2
public abstract class AbstractRESTClientService<E extends IDable<String>> 
    implements GenericService<E, String> { 


    private Class<E> classType; 
    private Class<E[]> classArray; 
    private String controllerPath; 

    public AbstractRESTClientService(final Class<E> classType, 
     final Class<E[]> classArray, 
     final String controllerPath) { 
      this.classType = classType; 
      this.classArray = classArray; 
      this.controllerPath = controllerPath; 
      this.webService = new GenericWebClientService<E>(); 
     } 

    // ... other methods 
} 

有沒有一種方法來操縱classType所成classArray這樣我不需要classArray以作爲參數傳遞?嫺熟類<E>成Class <E[]>

+0

在理論上,你可以做'類 arrClazz =(類))Array.newInstance(clazz中,0).getClass();',但我不知道是不是最好的方法 – user902383

回答

1

我不想認爲這是做到這一點的最佳方式,但以下是我如何處理它。

public abstract class AbstractRESTClientService<E extends IDable<String>> 
    implements GenericService<E, String> { 


private Class<E> classType; 
private Class<E[]> classArray; 
private String controllerPath; 

@SuppressWarnings("unchecked") 
public AbstractRESTClientService(final Class<E[]> classArray, 
    final String controllerPath) { 
     this.classType = (Class<E>) classArray.getComponentType(); 
     this.classArray = classArray; 
     this.controllerPath = controllerPath; 
     this.webService = new GenericWebClientService<E>(); 
    } 

    // ... other methods 
} 
相關問題