如何从泛型类型参数中获取`.class`属性?

编程入门 行业动态 更新时间:2024-10-26 04:22:50
本文介绍了如何从泛型类型参数中获取`.class`属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此问题的接受答案描述了如何创建 T > Generic< T> 类。这涉及将 Class 参数传递给 Generic 构造函数,并调用 newInstance 方法。

然后创建一个新的通用< Bar> 实例,并且参数 Bar .class 被传入。

如果新的 Generic class不是像 Bar 这样的已知类,但它本身就是一个泛型类型参数?假设我有一些其他的类 Skeet< J> ,并且我想创建一个新的 Generic< J> 在那个班里面然后,如果我尝试传入 J.class ,我得到以下编译器错误:

不能从类型变量中选择。

有没有解决方法?

触发错误的代码的特定位是:

public class InputField< W extends Component& WidgetInterface> 扩展了InputFieldArray< W> { public InputField(String labelText) { super(new String [] {labelText},W.class); } / * ... * / } public class InputFieldArray< W extends Component& WidgetInterface> extends JPanel { / * ... * / public InputFieldArray(String [] labelText,Class< W> clazz) throws InstantiationException,IllegalAccessException $ b $ * b for(int i = 0; i 错误发生,因为我不能写 W.class 。还有其他方式传递相同的信息吗? 使用 .class 不允许在类型参数上使用 .class 由于输入删除, W 将在运行时被擦除为 Component 。 InputField 还需要从调用方获取 Class ,例如 InputFieldArray :

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

The accepted answer to this question describes how to create an instance of T in the Generic<T> class. This involves passing in a Class<T> parameter to the Generic constructor and callin the newInstance method from that.

A new instance of Generic<Bar> is then created, and the parameter Bar.class is passed in.

What do you do if the generic type parameter for the new Generic class is not some known class like Bar but is itself a generic type parameter? Suppose I had some other class Skeet<J> and I wanted to create a new instance of Generic<J> from inside that class. Then, if I try to pass in J.class I get the following compiler error:

cannot select from a type variable.

Is there any way around this?

The specific bit of code triggering the error for me is:

public class InputField<W extends Component & WidgetInterface> extends InputFieldArray<W> { public InputField(String labelText) { super(new String[] {labelText}, W.class); } /* ... */ } public class InputFieldArray<W extends Component & WidgetInterface> extends JPanel { /* ... */ public InputFieldArray(String[] labelText, Class<W> clazz) throws InstantiationException, IllegalAccessException { /* ... */ for (int i = 0 ; i < labelText.length ; i++) { newLabel = new JLabel(labelText[i]); newWidget = clazz.newInstance(); /* ... */ } /* ... */ } /* ... */ }

The error happens, because I can't write W.class. Is there some other way of passing in the same information?

解决方案

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<W> from the caller, like InputFieldArray:

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

更多推荐

如何从泛型类型参数中获取`.class`属性?

本文发布于:2023-11-12 11:39:45,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   参数   类型   class

发布评论

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

>www.elefans.com

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