型パラメーターを使用してクラスを初期化する際に問題が発生しています。これは 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 にコンパイルされています。