我正在尝试为依赖于资源类类型的 Mapper 接口构建一个通用工厂。
这是代码:
public interface Resource { }
public class IntegerResource implements Resource { }
public class IntegerMapper implements Mapper<IntegerResource> {
}
public static <R extends Resource> Mapper<R> buildMapper(Class<R> clazz){
if (IntegerResource.class.equals(clazz)){
return new IntegerMapper();//Compilation error: Type mismatch: cannot convert from IntegerMapper to Mapper<R>
}
return null;
}
public <R extends Resource> R getResource(Class<R> clazz){
Mapper<R> m = buildMapper(clazz);
return null;
}