如何存储变量以使其无法修改?

时间:2015-04-25 18:34:54

标签: java

很抱歉,如果我弄错了这个问题的格式,这是我的第一篇文章。

基本上我的问题是我将一个对象指定为变量,然后我更改原始对象,然后尝试使用该变量将对象恢复回原始状态。但是,我经常发现,尽管从来没有在变量上调用一个方法,变量本身已经改变为与现在的对象完全相同。

我尝试存储的对象是b,看似改变的变量都是存储和原始的。我在原创上调用方法,但没有人应该改变它,只是从中接收信息。我没有在店里打电话。

当我尝试执行b = store时如何;我可以确保b与参数中传递给它的b相同吗?

该方法的代码如下:

public int getMove(Board b, int playerNum) throws QuitGameException{        
    original = b;
    store = b;
    int otherPlayerNum = 0;
    try{
        out.println("In house 2 on our side " + original.getSeeds(2, playerNum));
    } catch (Exception e){
        out.println("problem");
    }
    try{
        out.println("In house 2 on our side " + store.getSeeds(2, playerNum));
    } catch (Exception e){
        out.println("problem");
    }


    //Prints the current board to the user in a textual interface
    if(playerNum == 1){
        otherPlayerNum = 2;
    } else {
        otherPlayerNum = 1;
    }

    out.println("Opponent's side: ");
    for(int i=6;i>0;i--){
        try{
            out.print("House " + i + " : [" + original.getSeeds(i, otherPlayerNum)+ "] ");
        } catch (InvalidHouseException e){
            out.print("Invalid house");
        }
    }
    out.println();
    out.println("Opponent's score: " + original.getScore(otherPlayerNum));

    out.println("Computer's side: ");
    for(int i=1;i<7;i++){
        try{
            out.print("House " + i + " : [" + original.getSeeds(i, playerNum) + "] ");
        } catch (InvalidHouseException e){
            out.print("Invalid house");
        }
    }
    out.println();
    out.println("Computer's score: " + original.getScore(playerNum));

    //Each move is tried so that the score can be received, the move which produces the highest score is chosen to be returned.
    System.out.println(b.toString());
    int highestScore = b.getScore(playerNum);
    int bestHouse = 0;
    boolean moveFound = false;
    int move = 1;
    int score = 0;
    for(int i =1; i<7 ;i++){
        try{
            b.makeMove(i,playerNum);
            score = b.getScore(playerNum);
        } catch (Exception e){
            out.println("a problem");
            score = 0;
        }

        if(score>highestScore){
            bestHouse = i;
            highestScore = score;
            moveFound = true;
            move = i;
        }
        try{
            System.out.println("Seeds in side " + playerNum + " and house " +i+" = "+original.getSeeds(i, playerNum));
        } catch (Exception e){
            out.println("problem");
        }            
        b = original;
    }
    try{
        out.println("In house 2 on our side " + original.getSeeds(2, playerNum));
    } catch (Exception e){
        out.println("problem");
    }
    try{
        out.println("In house 2 on our side " + store.getSeeds(2, playerNum));
    } catch (Exception e){
        out.println("problem");
    }
    out.println("All okay? "+b.equals(store));
    out.println("All okay? "+original.equals(store));
    int side = playerNum;
    int bestScore = 0;
    int seeds = 0;

    //If no move has been found which increases the score then the first house with a seed in it is chosen to be returned
    if(!moveFound){
        for (int i =1; i<7 ;i++){
            try{
                seeds = original.getSeeds(i,playerNum);
                System.out.println("Seeds = "+ seeds);
                if (seeds>0 && !moveFound){
                    move = i;
                    moveFound = true;
                }
            } catch (Exception e){
                seeds = 0;
            }
        }
    }
    return move;

}

提前谢谢你。我很乐意提供进一步的细节。

1 个答案:

答案 0 :(得分:4)

变量不包含对象。它们将引用(或指针,如果您愿意)保存到对象。将对象分配给变量不会创建对象的任何副本。它只是使变量指向这个对象:

Board b = new Board();

这将创建一个Board对象,并使b变量指向此对象:

                b ------> board object

Board store = b;

这将同一个板对象分配给另一个变量store。因此,bstore现在都指向了棋盘对象。

                b ------> board object
                             ^
                store -------|

因此,如果您执行类似b.setName("new name")之类的操作,则修改板对象的状态,并且由于b和存储两者都引用相同的板对象,因此调用store.getName()将返回新名称:

                b ------> board object with new name
                             ^
                store -------|

如果您希望变量保持原始电路板状态,则需要创建电路板对象的副本:

Board store = new Board(b);

这个构造函数应该从作为参数的板上复制所有内容:

public Board(Board other) {
    this.name = other.getName();
    ...
}
相关问题