我在使用类型参数初始化类时遇到问题。这似乎是 Java 类型推断的一个缺点,我想知道是否有解决方法或更好的方法来实现这一点。
public class ParentModel {}
public class ChildModel extends ParentModel {}
public class Service<E extends ParentModel, T extends Collection<E>> {
private Class<T> classOfT;
private Class<E> classOfE;
public Service(Class<E> classOfE, Class<T> classOfT) {
this.classOfE = classOfE;
this.classOfT = classOfT;
}
}
public class BusinessLogic {
public void someLogic() {
Service<ChildModel, ArrayList<ChildModel>> service = new
Service<ChildModel, ArrayList<ChildModel>>(ChildModel.class, ArrayList.class);
}
}
编译时错误在BusinessLogic::someLogic()
:
构造函数 Service<ChildModel, ArrayList<ChildModel>>(Class<ChildModel>, Class<ArrayList>) 未定义
编译为 Java 7。