简体   繁体   English

如何使用也键入的类型参数创建java.lang.Class?

[英]How to create java.lang.Class with type parameter that is also typed?

What to put on place of ??? 该怎么做? in the following code so it would work? 在下面的代码中,它会工作吗?

import java.util.List;

public class Generic {

    private static class Foo<T> {
        Foo(Class<T> clazz) {
            assert clazz != null;
        }
    }

    public static void main(String[] args) {
        Class<List<String>> x = ???;
        Foo<List<String>> t = new Foo<List<String>>(x);
    }

}

I would go for: 我会去:

@SuppressWarnings("unchecked")
Class<List<String>> klass = (Class<List<String>>)((Class<?>) List.class);

And you do not need an instance of a the class for this type of cast. 并且您不需要此类类型的强制类的实例。

在运行时,所有List “类”都相同,因为擦除了泛型类型

Class<List<String>> x = (Class<List<String>>) (Class) List.class;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM