【发布时间】:2010-10-16 19:32:39
【问题描述】:
我正在尝试为对象编写一个 equals 方法来比较它们的字段并在它们相等时返回 true。
private int x, y, direction;
private Color color;
public boolean equals(Ghost other){
if (this.x == other.x && this.y == other.y &&
this.direction == other.direction && this.color == other.color)
return true;
else
return false;
}
这有什么问题?
【问题讨论】:
-
@fprime:你只需要一个return语句,不需要
if。 ;-) -
在 if 语句之前打印出单个 if 短语(例如
this.x == other.x)的结果。看看哪些失败了。 -
是否有任何变量被比较对象类型,而不是原始类型?
-
现在等等,我在做的是比较内存地址还是实际值?
-
它会比较值,你有没有运行你的代码看看它是否有效?