【发布时间】:2013-12-24 21:19:09
【问题描述】:
这是对以前帖子的重写。我现在写了一个完整的新项目,只是为了展示我不理解的行为。下面你会看到代码。该问题在本文末尾的测试输出中变得可见。我的假设是错误位于标记为 /* PROBLEMATIC */ 的行,但我不知道这会是什么错误。
我想做的是这样的:
创建一个板对象boardA
创建另一个名为 boardB,它是 boardA 的深层副本,因此它在其数组 pos[][] 中引用不同的 Square 对象(正如输出告诉我的那样,确实如此)。
因此,当我输出对象 id(使用 hashCode())(如在第二个 Board 复制构造函数中)时,我希望为每个对象(我确实得到)获得不同的哈希值,并且为父对象获得不同的哈希值每个 Square 对象(我不这样做)。我希望输出澄清了这一点。
有什么想法吗? 谢谢。
这是完整的测试代码:
public class CloneTest {
public static void main(String[] args) {
Board boardA = new Board();
Board boardB = new Board(boardA.pos);
System.out.println("boardA=" + boardA.hashCode());
System.out.println("boardB=" + boardB.hashCode());
}
}
class Board {
final int boardSize = 2;
Square[][] pos;
/* create empty board */
public Board() {
pos = new Square[boardSize][boardSize];
for(int row=0; row < boardSize; row++)
for(int col=0;col < boardSize; col++) {
pos[row][col] = new Square(row,col);
}
}
/* copy existing board */
public Board(Square[][] initpos) {
pos = new Square[boardSize][boardSize];
for(int row=0; row < boardSize; row++)
for(int col=0;col < boardSize; col++) {
System.out.println("initpos["+row+"]["+col+"]=" + initpos[row][col].hashCode() +
" gh=" + initpos[row][col].gameHash());
try {
this.pos[row][col] = initpos[row][col].clone(); /* PROBLEMATIC? */
} catch (CloneNotSupportedException e) {
}
System.out.println("this.pos["+row+"]["+col+"]=" + this.pos[row][col].hashCode() +
" gh=" + this.pos[row][col].gameHash());
}
}
class Square implements Cloneable {
int row;
int col;
public Square(int r, int c) {
row = r;
col = c;
}
public Square clone() throws CloneNotSupportedException {
Square newsquare = (Square) super.clone(); /* WRONG?*/
return newsquare;
}
public int gameHash() {
return Board.this.hashCode();
}
}
}
这是一个示例输出:
initpos[0][0]=1598553873 gh=1874519541
this.pos[0][0]=1464824097 gh=1874519541
initpos[0][1]=546069071 gh=1874519541
this.pos[0][1]=1585252666 gh=1874519541
initpos[1][0]=1659432780 gh=1874519541
this.pos[1][0]=716609871 gh=1874519541
initpos[1][1]=973809521 gh=1874519541
this.pos[1][1]=843745660 gh=1874519541
boardA=1874519541
boardB=998786479
如您所见,initpos[0][0] 和 this.pos[0][0] 行的 gh 值相同 (1874519541)。 我希望 this.pos[0][0] 的 gh-value 为 998786479,即 boardB 的 id。
所以克隆的 Square 对象(它应该有不同的 id)仍然认为它属于第一个 Board 对象。但是,既然我将它分配给构造函数中的新 Board 对象,那怎么可能呢?
【问题讨论】: