【发布时间】:2012-05-15 06:19:09
【问题描述】:
给定类值:
public class Value {
private int xVal1;
private int xVal2;
private double pVal;
// constructor of the Value class
public Value(int _xVal1 ,int _xVal2 , double _pVal)
{
this.xVal1 = _xVal1;
this.xVal2 = _xVal2;
this.pVal = _pVal;
}
public int getX1val()
{
return this.xVal1;
}
...
}
我正在尝试使用 reflection 创建该类的新实例:
来自主要:
.... // some code
....
....
int _xval1 = Integer.parseInt(getCharacterDataFromElement(line));
int _xval2 = Integer.parseInt(getCharacterDataFromElement(line2));
double _pval = Double.parseDouble(getCharacterDataFromElement(line3));
Class c = null;
c = Class.forName("Value");
Object o = c.newInstance(_xval1,_xval2,_pval);
...
这不起作用,Eclipse 的输出:The method newInstance() in the type Class is not applicable for the arguments (int, int, double)
如果是这样,我如何使用 reflection 创建一个新的 Value 对象,并在其中调用 Value 的 Constructor ?
谢谢
【问题讨论】:
标签: java reflection constructor