将类<e>精细化为类<e []>

时间:2016-03-17 10:33:32

标签: java generics

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作为参数传递?

1 个答案:

答案 0 :(得分: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
}