是否可以使用类对象声明泛型的类型?在运行时用类对象定义泛型
举例来说,我愿做这样的事情:
Class returnType = theMethod.getReturnType();
AttributeComponent<returnType> attComponent;
attComponent = new AttributeComponent<returnType>(returnType, attName);
attributeComponents.put(methodName.substring(3), attComponent);
现在我知道这显然是不正确的,但有没有办法实现这一目标?
编辑:解释一点点
我使用反射来遍历所有干将,然后生成每个属性(在AttributeComponent类,它有一个JComponent元素和JLabel)UI元素。我想使用泛型来创建一个getValue()方法,该方法将返回属性类型的对象。
你不能那样做。由于'returnType'是变量,变量不能用作通用参数。 –