设置泛型类类型
如何设置泛型类型?例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简而言之,你想要的是
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
由于类型擦除,这是不可能的。各种库中有许多技巧可以帮助反射,但语言本身通常对泛型类型的内省支持很差。
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.