Java 精细级<;E>;进入课堂<;E[]和gt; 公共抽象类AbstractRESTClientService 实现泛型服务{ 私有类类型; 私有类类数组; 私有字符串控制器路径; 公共AbstractRESTClientService(最终类类类型, 最后一个类classArray, 最终字符串控制器路径){ this.classType=classType; this.classArray=classArray; this.controllerPath=controllerPath; this.webService=新的GenericWebClient服务(); } //…其他方法 }
有没有一种方法可以将classType操作到classArray中,这样我就不需要将classArray作为参数传入?我不想认为这是最好的方法,但下面是我如何处理它的Java 精细级<;E>;进入课堂<;E[]和gt; 公共抽象类AbstractRESTClientService 实现泛型服务{ 私有类类型; 私有类类数组; 私有字符串控制器路径; 公共AbstractRESTClientService(最终类类类型, 最后一个类classArray, 最终字符串控制器路径){ this.classType=classType; this.classArray=classArray; this.controllerPath=controllerPath; this.webService=新的GenericWebClient服务(); } //…其他方法 },java,generics,Java,Generics,有没有一种方法可以将classType操作到classArray中,这样我就不需要将classArray作为参数传入?我不想认为这是最好的方法,但下面是我如何处理它的 public abstract class AbstractRESTClientService<E extends IDable<String>> implements GenericService<E, String> { private Class<E> c
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
}
公共抽象类AbstractRESTClientService
实现泛型服务{
私有类类型;
私有类类数组;
私有字符串控制器路径;
@抑制警告(“未选中”)
公共AbstractRESTClientService(最终类classArray,
最终字符串控制器路径){
this.classType=(类)classArray.getComponentType();
this.classArray=classArray;
this.controllerPath=controllerPath;
this.webService=新的GenericWebClient服务();
}
//…其他方法
}
从理论上讲,您可以执行类arrClazz=(Class))Array.newInstance(clazz,0.getClass()代码>但我不确定这是最好的方法
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
}