【发布时间】:2012-10-08 08:48:18
【问题描述】:
是否可以使用类对象来声明泛型的类型?
例如,我想做这样的事情:
Class returnType = theMethod.getReturnType();
AttributeComponent<returnType> attComponent;
attComponent = new AttributeComponent<returnType>(returnType, attName);
attributeComponents.put(methodName.substring(3), attComponent);
现在我知道这显然是不正确的,但有没有办法做到这一点?
编辑:稍微解释一下
我使用反射来遍历所有 getter,然后为每个属性生成一个 UI 元素(AttributeComponent 类,它有一个 JComponent 元素和一个 JLabel)。我想使用泛型来创建一个 getValue() 方法,该方法将返回属性类型的对象。
【问题讨论】:
-
你不能那样做。由于
returnType是变量,变量不能用作泛型参数。
标签: java generics reflection