gpt4 book ai didi

java - 如何存储一个变量使其不能被修改?

转载 作者:行者123 更新时间:2023-11-29 03:10:05 26 4
gpt4 key购买 nike

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

基本上我的问题是我将一个对象分配为一个变量,然后更改原始对象,然后尝试使用该变量将对象恢复到其原始状态。然而,我不断发现,尽管从未对变量调用方法,但变量本身已更改为与对象现在的样子完全相同。

我尝试存储的对象是 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;

}

提前致谢。我很乐意提供更多详细信息。

最佳答案

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

Board b = new Board();

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

                b ------> board object

Board store = b;

这会将同一个棋盘对象分配给另一个变量 store。所以 bstore 现在都指向 board 对象。

                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();
...
}

关于java - 如何存储一个变量使其不能被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com