获取泛型类型
Get type of Generics
我需要获取泛型类型。
我已经尝试关注
public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {
public RetrofitRequest(){
super(Response<RESULT>.class); //compile time error
}
@Override
public final Response<RESULT> loadDataFromNetwork() throws Exception {
WebService webService = RestFulWebService.getRestFulWebService();
return doInBackground(webService);
}
protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;
}
知道如何完成这项工作吗?提前致谢!!!
和SpiceRequest
是这样的
public abstract class SpiceRequest<RESULT> implements Comparable<SpiceRequest<RESULT>> {
public SpiceRequest(Class<RESULT> clazz) {
....
}
}
谢谢!
-supunz
您需要在请求 Class<RESULT>
时镜像您的父控制器。
public RetrofitRequest(Class<RESULT> clazz){
super(clazz);
}
令人遗憾的是,现在您必须沿着继承链一直沿用它,但是如果您想要实例化它,您还需要提供 class:
RetrofitRequest<String> impl = new RetrofitRequestImpl<String>(String.class);
在 RC 评论的帮助下解决了自己的问题
public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {
protected RetrofitRequest(){
super((Class<Response<RESULT>>) (Class<?>) Response.class);
}
@Override
public final Response<RESULT> loadDataFromNetwork() throws Exception {
WebService webService = RestFulWebService.getRestFulWebService();
return doInBackground(webService);
}
@WorkerThread
protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;
}
和https://coderanch.com/t/681438/java/Class-Type-List-java#3196253拯救了我的一天
我需要获取泛型类型。 我已经尝试关注
public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {
public RetrofitRequest(){
super(Response<RESULT>.class); //compile time error
}
@Override
public final Response<RESULT> loadDataFromNetwork() throws Exception {
WebService webService = RestFulWebService.getRestFulWebService();
return doInBackground(webService);
}
protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;
}
知道如何完成这项工作吗?提前致谢!!!
和SpiceRequest
是这样的
public abstract class SpiceRequest<RESULT> implements Comparable<SpiceRequest<RESULT>> {
public SpiceRequest(Class<RESULT> clazz) {
....
}
}
谢谢!
-supunz
您需要在请求 Class<RESULT>
时镜像您的父控制器。
public RetrofitRequest(Class<RESULT> clazz){
super(clazz);
}
令人遗憾的是,现在您必须沿着继承链一直沿用它,但是如果您想要实例化它,您还需要提供 class:
RetrofitRequest<String> impl = new RetrofitRequestImpl<String>(String.class);
在 RC 评论的帮助下解决了自己的问题
public abstract class RetrofitRequest<RESULT> extends SpiceRequest<Response<RESULT>> {
protected RetrofitRequest(){
super((Class<Response<RESULT>>) (Class<?>) Response.class);
}
@Override
public final Response<RESULT> loadDataFromNetwork() throws Exception {
WebService webService = RestFulWebService.getRestFulWebService();
return doInBackground(webService);
}
@WorkerThread
protected abstract Response<RESULT> doInBackground(WebService webService) throws Exception;
}
和https://coderanch.com/t/681438/java/Class-Type-List-java#3196253拯救了我的一天