[英]How to store a variable so that it can't be modified?
抱歉,如果我弄错了这个问题的格式,这是我的第一篇文章。
本质上,我的问题是我将一个对象分配为变量,然后更改原始对象,然后尝试使用该变量将对象恢复到其原始状态。 但是,尽管从未对变量调用任何方法,但我不断发现,变量本身已更改为与对象现在看起来完全相同的变量。
我尝试存储的对象是b,看似更改的变量既是store又是原始变量。 我在原始方法上调用方法,但是任何方法都不能更改它,只需从中获取信息即可。 我没有在商店中调用任何方法。
如何,当我尝试执行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
。 因此, b
和store
现在都指向板对象。
b ------> board object
^
store -------|
因此,如果您执行类似b.setName("new name")
,那么您将修改板对象的状态,并且由于b
和store都引用同一个板对象,因此调用store.getName()
将返回新名称:
b ------> board object with new name
^
store -------|
如果要使变量保持原始板状态,则需要创建板对象的副本:
Board store = new Board(b);
此构造函数应从作为参数的木板复制所有内容:
public Board(Board other) {
this.name = other.getName();
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.