设置泛型类类型

发布于 2024-12-19 12:25:40 字数 166 浏览 1 评论 0原文

如何设置泛型类型?例如:

Class<List<Integer>> asd = List<Integer>.class \\ does not work

可能这是一个有点愚蠢的问题,但我从未遇到过完成此操作的代码。

How can I set generic type? For example:

Class<List<Integer>> asd = List<Integer>.class \\ does not work

May be that is a bit stupid question, but I have never met a code where this thing was done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-12-26 12:25:40

简而言之,你想要的是List.class;泛型类型仅在编译时可用,并在运行时被“擦除”。这里有一个简短的解释: http://docs.oracle.com/javase /tutorial/java/generics/erasure.html
...还有更多详细信息: http://www.artima.com /weblogs/viewpost.jsp?thread=208860

In short, what you want is List.class; the generic types are only available at compile-time, and are "erased" at run-time. There's a brief explanation here: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
...and a bit more detail here: http://www.artima.com/weblogs/viewpost.jsp?thread=208860

山有枢 2024-12-26 12:25:40

由于类型擦除,这是不可能的。各种库中有许多技巧可以帮助反射,但语言本身通常对泛型类型的内省支持很差。

This is not possible, due to type erasure. There's a number of hacks in various libraries to help out with reflection, but the language itself has generally poor support for introspection of generic types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文