How does one get a parameterized Class object to be used as a method argument?
class A
{
public A(Class c)
{
}
void main()
{
A a1 = new A(String.class); // OK
A> a2 = new A>(List.class); // error
A> a3 = new A>(Class>); // error
}
}
Why do I want to do that, you may ask? I have a parameterized class whose type is another parameterized class, and whose constructor requires that other class type as an argument. I understand that runtime classes have no information on their type parameters, but that shouldn't prevent me from doing this at compile time. It seems that I should be able to specify a type such as `List.class`. Is there another syntax to do this?
Here is my real usage case:
public class Bunch
{
Class type;
public Bunch(Class type)
{
this.type = type;
}
public static class MyBunch extends Bunch>
{
Class individualType;
// This constructor has redundant information.
public MyBunch(Class individualType, Class> listType)
{
super(listType);
this.individualType = individualType;
}
// I would prefer this constructor.
public MyBunch(Class individualType)
{
super( /* What do I put here? */ );
this.individualType = individualType;
}
}
}
Is this possible?
以上就是Generics: Creating parameterized class argument的详细内容,更多请关注web前端其它相关文章!