Java 检查X和Y坐标的equals()方法

Java 检查X和Y坐标的equals()方法,java,line,coordinates,equals,point,Java,Line,Coordinates,Equals,Point,好了,伙计们,首先很抱歉,如果这个代码很混乱,如果我的equals()完全错误,但这是我第一次使用它 我试图创建一个equals方法来检查两条线是否相等,如果两个端点相同,两条线被定义为相等 我的第一个问题是,我是否已经接近Point类中的方法,以及如何从Line类调用Point类中的equals()方法 谢谢你的帮助 public class Point { private int x; private int y; public Point( int x, int y) { th

好了,伙计们,首先很抱歉,如果这个代码很混乱,如果我的equals()完全错误,但这是我第一次使用它

我试图创建一个equals方法来检查两条线是否相等,如果两个端点相同,两条线被定义为相等

我的第一个问题是,我是否已经接近Point类中的方法,以及如何从Line类调用Point类中的equals()方法

谢谢你的帮助

public class Point {

private int x;
private int y;

public Point( int x, int y) {
    this.x = x;
    this.y = y;
}

public int getX() {
    return x;
}
public int getY() {
    return y;
}

public String toString() {
    return "x=" + x + ", y=" + y;
}

public boolean equals(Object o) {
      if (!(o instanceof Point)) {
            return false;
        }
        return (x == ((Point) o).x && y == ((Point) o).y);
}
}
}

对于返回this.y,它表示“无法访问的代码”。我的对象应该是“点”还是“线”

}

我更新了point类中的equals(),但从Line类调用它时仍然遇到问题,这会是类似的修复吗


感谢所有的帮助。

它说的是无法访问的代码,因为您的equals()方法将在
上完成执行,并返回这个.x==((点)o).x

尝试使用:

public boolean equals(Object o) {
    if(this.x == ((Point)o).x && this.y == ((Point)o).y) {
        return true;
    }
    return false;
}
可缩短为:

public boolean equals(Object o) {
    return this.x == ((Point)o).x && this.y == ((Point)o).y;
}
应该是:

public class Point {
    public boolean equals(Object o) {
        return (this.x == ((Point)o).x) && (this.y == ((Point)o).y);
    }
}
这样,您的代码就不会无法访问

您还必须检查:

if (!(o instanceof Point)) return false;

在第一次
return
调用后,该方法退出,因此第二次调用永远不会被计算

使用
return(this.x==((点)o.x)和&(this.y==((点)o.y)取而代之

public class Point {
    public boolean equals(Object o) {
        if(o == null) return false;
        if(!(o instanceOf Point) return false;
        return (this.x == ((Point)o).x) && (this.y == ((Point)o).y);
    }
}

你不可能有两个返回语句一个接一个,因为第二个总是不可访问的。您可以使用此选项,计算
x
是否等于
o.x
y
是否等于
o.y

 public boolean equals(Object o) {
        return this.x == ((Point)o).x && this.y == ((Point)o).y;
    }

“return”语句退出该方法,这就是为什么再往下的任何代码都无法访问的原因。此外,最好检查传递的对象是否属于正确的类,以避免类强制转换异常:

public boolean equals(Object o) {
    if (o instanceof Point) {
        Point po = (Point) o;
        return this.x == po.x && this.y == po.y;
    }
    return false;
}
参数必须是“Object”而不是“Point”,因此此方法将覆盖您正在使用的Object.equals方法

return this.x == ((Point)o).x;
return this.y == ((Point)o).y;
此代码将在检查x后返回,但永远不会到达y

因此,为了使代码正确工作,请使用

return (this.x == ((Point)o).x)&&(this.y == ((Point)o).y);

此代码将同时检查x和y,然后返回

这是不可访问的代码,因为您刚刚通过退出该方法而退出。您可能是指
this.x==(点)o.x&&this.y==(点)o.y

您应该有如下内容:

@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Point)) {
        return false;
    }
    return (x == ((Point) o).x && y == ((Point) o).y);
}
另外,对于
类,比较相关字段(
beg
end


当您重写
equals
(和
hashCode
)时,总是成对地编写它们)最好使用
@override
注释,以防您错误地编写
公共布尔等于(点o)
(请注意参数是
,而不是
对象
),因此,编译器将捕获它。

如果重写equals,那么还应该/必须重写hashCode()


您可以查看。查看此项了解更多信息。您应该处理
o
null
的情况。所有这些答案都应该是正确的。
null
永远不是类的实例。因此,instanceof check将正确处理
null
。显式地检查
null
只是一个风格问题(和微性能优化)。好的,谢谢,这解决了我的问题,但是我仍然无法从Line类中正确调用它?
@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Point)) {
        return false;
    }
    return (x == ((Point) o).x && y == ((Point) o).y);
}
@Override
public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!(o instanceof Line)) {
        return false;
    }
    return (beg == ((Line) o).beg && end == ((Line) o).end);
}
public boolean equals(Object o) {
   if(o==this){return true;}
   if(!o instanceof Point){return false;}
   Point p = (Point)o;
   return (this.x==p.x && this.y==p.y);
}