如何访问Collection Generic Type类(How to access Collection Generic Type class)

编程入门 行业动态 更新时间:2024-10-27 11:24:48
如何访问Collection Generic Type类(How to access Collection Generic Type class)

我有一个这样的课:

public class SubClass <T> extends SuperClass<Collection<T>>{ public Class<T> getInner() { // What goes here? } }

SuperClass只是一个通用类。 我的问题是如何获得对Class对象的访问权以便返回它? 由于Type擦除,我似乎无法做到这一点。

有帮助吗?

I have a class like this :

public class SubClass <T> extends SuperClass<Collection<T>>{ public Class<T> getInner() { // What goes here? } }

Where SuperClass is simply a generic class. My question is how to gain access to a Class object in order to return it? I can't seem to be able to do this because of Type erasure.

Any help?

最满意答案

确切地说,类型擦除意味着您必须明确地执行此操作。

基本上你需要SubClass将Class<T>作为构造函数中的参数,然后挂起它。 如果T实际上意味着(比如) List<String> ,你仍然可以在那里找到类型擦除问题。 有关此问题的更多信息和解决方法,请参阅Neal Gafter的博客 ,以及用于实现的Guice 2.0 TypeLiteral类 。

Exactly, type erasure means you have to do it explicitly.

Basically you need SubClass to take a Class<T> as a parameter in the constructor, and then hang onto it. You can still end up with type erasure issues there though, if T is actually meant to be (say) List<String>. See Neal Gafter's blog for more info on this and a workaround, and the Guice 2.0 TypeLiteral class for an implementation.

更多推荐

本文发布于:2023-07-29 14:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Generic   Collection   Type   class   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!