How do I get the `.class` attribute from a generic type parameter?

前端 未结 3 1970
星月不相逢
星月不相逢 2021-02-03 16:44

The accepted answer to this question describes how to create an instance of T in the Generic class. This involves passing in a Class<

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 17:13

    Using .class on a type parameter isn't allowed - because of type erasure, W will have been erased to Component at runtime. InputField will need to also take a Class from the caller, like InputFieldArray:

    public InputField(String labelText, Class clazz)
    {
        super(new String[] {labelText}, clazz);
    }
    

提交回复
热议问题