Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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_Class_Generics_Parameters - Fatal编程技术网

Java-创建类对象作为泛型参数的泛型列表?

Java-创建类对象作为泛型参数的泛型列表?,java,class,generics,parameters,Java,Class,Generics,Parameters,假设我有一个对象类c。可以用c作为泛型参数创建泛型列表吗 比如说: Class<?> c = doSomething(); List<c> list = new ArrayList<c>(); Class c=doSomething(); 列表=新的ArrayList(); 不,这是不可能的-至少你的语法不会编译。但是,您可以尝试学习Java中的泛型,看看这是否有助于您的具体案例,因为这可能是一个a-B问题 例如,这项工作: <T> in

假设我有一个对象
类c
。可以用c作为泛型参数创建泛型列表吗

比如说:

Class<?> c = doSomething();
List<c> list = new ArrayList<c>();
Class c=doSomething();
列表=新的ArrayList();

不,这是不可能的-至少你的语法不会编译。但是,您可以尝试学习Java中的泛型,看看这是否有助于您的具体案例,因为这可能是一个a-B问题

例如,这项工作:

    <T> int yourFunction(List<T> items) {
        T item = items.get(0);
        // play with the item of type T, yeah!
    }
int函数(列表项){
T item=items.get(0);
//玩T型物品,耶!
}

不,这是不可能的-至少你的语法不会编译。但是,您可以尝试学习Java中的泛型,看看这是否有助于您的具体案例,因为这可能是一个a-B问题

例如,这项工作:

    <T> int yourFunction(List<T> items) {
        T item = items.get(0);
        // play with the item of type T, yeah!
    }
int函数(列表项){
T item=items.get(0);
//玩T型物品,耶!
}

对于
列表
可能就是您想要的。任何对反射的使用,包括
通常都是错误的。

对于
列表
可能就是您想要的。任何反射的使用,包括
通常都是错误的。

如果你能做到这一点,它就没有用了。泛型类型仅在编译之前有用。如果你能做到这一点,你就不能用它做任何事情。如果你能做到这一点,它将毫无用处。泛型类型仅在编译之前有用。如果你能做到这一点,你就不能用它做任何事你不能用
列表
做任何事。