我有一个这样的课:
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.
更多推荐
发布评论