给定班级回合:
public class Round {
private int roundNumber;
private Door door1;
private Door door2;
public Round(int _roundNumber)
{
this.roundNumber = _roundNumber;
}
public void setRoundNumber(int _number)
{
this.roundNumber = _number;
this.door1 = null;
this.door2 = null;
}
public int getRoundNumber()
{
return this.roundNumber;
}
...
和 Main 中的代码:
Round[] gameRounds;
// manipulations on gameRounds , assume that we put some data into array gameRounds
...
...
Object ret = null;
for (int i = 0; i < gameRounds.length; i++)
{
Method roundFunction = Round.class.getMethod("getRoundNumber", new Class[] {});
ret = roundFunction.invoke(gameRounds[i]);
// need to put something here
}
我正在尝试roundNumber
使用反射检索字段,但返回值为 Object 类型,我该如何使用它的值,即如何将其转换为int roundNumber
?我需要将它写入一个新的 XML 文件...
谢谢