1

给定班级回合:

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 文件...

谢谢

4

1 回答 1

2

返回值将是一个Integer. 将其投射到一个Integer,然后让自动拆箱处理它。

于 2012-05-07T02:26:58.323 回答