Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-使用类型参数初始化类的问题_Java_Generics_Nested Generics - Fatal编程技术网

Java-使用类型参数初始化类的问题

Java-使用类型参数初始化类的问题,java,generics,nested-generics,Java,Generics,Nested Generics,初始化带有类型参数的类时遇到问题。这似乎是Java类型推断的一个缺点,我想知道是否有办法解决这个问题,或者有更好的方法实现这个问题 public class ParentModel {} public class ChildModel extends ParentModel {} public class Service<E extends ParentModel, T extends Collection<E>> { private Class<T&g

初始化带有类型参数的类时遇到问题。这似乎是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);
    }
}
公共类父模型{}
公共类ChildModel扩展了ParentModel{}
公务舱服务{
私有类classOfT;
私家班;
公共服务(等级OFE、等级OFT){
this.classOfE=classOfE;
this.classOfT=classOfT;
}
}
公共类业务逻辑{
公共逻辑{
服务=新的
服务(ChildModel.class、ArrayList.class);
}
}
编译时错误在
BusinessLogic::someLogic()
中:

构造函数服务(类,类)未定义


编译为Java 7。

由于没有
ArrayList.class这样的东西,因此没有优雅的方法来解决这个问题。
您可以将原始类型传递给构造函数,如Yassin所述,如下所示:

Service<ChildModel, ArrayList<ChildModel>> s1 = 
        new Service<>(ChildModel.class, (Class) ArrayList.class) 

这更详细,但我更喜欢原始类型(在这两种情况下,您都会收到编译器警告)

因为Java中的泛型是“通过擦除”实现的,没有
类>
,只有

您可以做的是允许超类型

Class<? super T> classOfT;
Class<? super E> classOfE;
public Service(Class<? super E> classOfE, Class<? super T> classOfT) {

这是同一个班级。在运行时,泛型实际上已经消失了

当您打算拥有
Class
字段或任何类型的
T
E
字段时,这个
Class clazz=(Class)ArrayList.Class不应该编译,是吗?我喜欢使用
super
的想法对不起,您需要一个双重演员阵容,可以使用
super
extensed
Class<? super T> classOfT;
Class<? super E> classOfE;
public Service(Class<? super E> classOfE, Class<? super T> classOfT) {
Class<ArrayList<Integer>> clazz =
    (Class<ArrayList<Integer>>) (Class<? super ArrayList>) 
    ArrayList.class;
ArrayList<Integer> a1 = new ArrayList<>();
ArrayList<Double> a2 = new ArrayList<>();
System.out.println(a1.getClass() == a2.getClass());