Java 为什么在我的蛇游戏中对象的比较返回false

Java 为什么在我的蛇游戏中对象的比较返回false,java,Java,职位: public class TestAsmt1 { public static void main(String[] args) { testPassed = true; theSnake = new Snake(1,1); if (!theSnake.snakePosition(new Position(1,1))) testPassed = false; 我不明白为什么当我比较对象时它返回false。testpassed对我来说是真的。我做错了什么?我已经想了至少两个小时了。特

职位:

public class TestAsmt1 {

public static void main(String[] args) {


testPassed = true;
theSnake = new Snake(1,1);
if (!theSnake.snakePosition(new Position(1,1))) testPassed = false;

我不明白为什么当我比较对象时它返回false。testpassed对我来说是真的。我做错了什么?我已经想了至少两个小时了。特别是看if(pos.equals(蛇形体[i])行。我昨天看到其他人也有这个问题(这个作业今晚午夜到期),但他们无法解决。知道问题是什么吗?

您正在做的是创建一个名为equals的函数,该函数将位置对象作为参数。您应该做的是重写现有的以对象为参数的equals函数,并实现hashCode函数,完成契约

为此,Position类需要包含final字段。您还需要重写我所述的函数。这可以通过以下方式实现

公共类职位{
私人最后一行;
私人最后一栏;
公共位置(整数行,整数列){
位置列=列;
位置行=行;
}
@凌驾
公共布尔等于(对象其他){
如果(其他==此){
返回true;
}
如果(位置的其他实例){
位置其他位置=(位置)其他;
返回this.positionRow==otherPosition.positionRow&&this.positionColumn==otherPosition.positionColumn;
}
返回true;
}
@凌驾
公共int hashCode(){
返回Objects.hash(positionRow,positionColumn);
}
}
值得注意的是,Snake#snakePosition函数也被破坏。循环遍历蛇身数组的所有索引,但只检查位置参数是否等于蛇身数组中索引为0的第一个对象。这就是改进的方法

公共布尔相交(位置位置){
返回Arrays.stream(snakeBody).filter(Objects::nonNull).anyMatch(body->body.equals(pos));
}
publicstaticvoidmain(字符串[]args){
Snake-theSnake=新蛇(1,1);
布尔testPassed=theSnake.intersects(新位置(1,1));
System.out.println(“测试通过:“+testPassed”);
}

您要做的是创建一个名为equals的函数,该函数将位置对象作为参数。您应该做的是重写现有的以对象为参数的equals函数,并实现hashCode函数,完成契约

为此,Position类需要包含final字段。您还需要重写我所述的函数。这可以通过以下方式实现

公共类职位{
私人最后一行;
私人最后一栏;
公共位置(整数行,整数列){
位置列=列;
位置行=行;
}
@凌驾
公共布尔等于(对象其他){
如果(其他==此){
返回true;
}
如果(位置的其他实例){
位置其他位置=(位置)其他;
返回this.positionRow==otherPosition.positionRow&&this.positionColumn==otherPosition.positionColumn;
}
返回true;
}
@凌驾
公共int hashCode(){
返回Objects.hash(positionRow,positionColumn);
}
}
值得注意的是,Snake#snakePosition函数也被破坏。循环遍历蛇身数组的所有索引,但只检查位置参数是否等于蛇身数组中索引为0的第一个对象。这就是改进的方法

公共布尔相交(位置位置){
返回Arrays.stream(snakeBody).filter(Objects::nonNull).anyMatch(body->body.equals(pos));
}
publicstaticvoidmain(字符串[]args){
Snake-theSnake=新蛇(1,1);
布尔testPassed=theSnake.intersects(新位置(1,1));
System.out.println(“测试通过:“+testPassed”);
}

对象的方法签名。等于(对象)不采用
位置
参数。你应该使用
@Override
注释和你的调试器(尽管这可能是一个棘手的问题)。我从来没有使用过@Override,我应该把它放在哪里。
对象的方法签名。equals(Object)
不带
位置
参数。你应该使用
@Override
注释和你的调试器(尽管这是一个棘手的问题),我应该把它放在哪里/为什么他们还需要实现
hashCode
函数?有一些很棒的文章介绍了为什么需要hashCode函数来完成equals/hashCode契约。我将在主要帖子中提供它们。为什么它们也需要实现
hashCode
函数?有一些很棒的文章介绍了为什么需要hashCode函数来完成equals/hashCode契约。我会在主帖子中提供它们。
public class TestAsmt1 {

public static void main(String[] args) {


testPassed = true;
theSnake = new Snake(1,1);
if (!theSnake.snakePosition(new Position(1,1))) testPassed = false;
public class Position {

    private int positionRow;
    private int positionColumn;

    public Position(int row, int column) {
        positionColumn = column;
        positionRow = row;
    }

    public boolean equals (Position otherPosition) {
        if(otherPosition.getRow() == this.getRow() && otherPosition.getCol() == this.getCol() ) {
            return true;
        } else {
        return false;
        }
    }


}