且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用反射实例化具有泛型类的java.util.ArrayList

更新时间:2021-10-03 16:59:00

object在执行时不知道它的泛型类型。只需创建一个原始类型的新实例( java.util.ArrayList )。目标对象不会知道差异(因为不是任何区别)。

An object doesn't know about its generic type at execution time. Just create a new instance of the raw type (java.util.ArrayList). The target object won't know the difference (because there isn't any difference).

基本上Java泛型是一个编译时的技巧,在编译的类中使用元数据,但只是在执行时进行转换。有关详细信息,请参阅 Java泛型常见问题解答

Basically Java generics is a compile-time trick, with metadata in the compiled classes but just casting at execution time. See the Java generics FAQ for more information.