在java中比较俩个类的实例是否相等不是使用”=”,而是使用equals,类默认是继承Object类中的equals方法,所以我们得重新定义equals方法
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Point that) {
return this.x == that.x && this.y == that.y;
}
}
测试:
Object p1 = new Point(1, 1);
Point p2 = new Point(1, 1);
System.out.println(p1.equals(p2)); // 显示 false
p1 使用Object声明,在使用时用的是Object的equals,可以使用@Override来避免
ublic class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point) {
Point p = (Point) that;
return this.x == p.x && this.y == p.y;
}
return false;
}
}
在做测试:
Object p1 = new Point(1, 1);
Point p2 = new Point(1, 1);
System.out.println(p1.equals(p2)); // 显示 true
在Object的equals中说到,实现equals方法要遵守下面四条约定
- 反身性(reflexive):x.equals(x)的结果要为true;
- 对称性(Symmetric):x.equals(y)与y.equals(x)结果要相同
- 传递性(Transitive):x.equals(y),y.equals(z)的结果都是true时,x.equals(z)结果也为true
- 一致性(Consistent):同一个执行期间,对x.equals(y)的多次调用,结果必须相同
接下来考虑继承的情况,再定一个Point3D:
public class Point3D extends Point {
public final int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point3D) {
Point3D p = (Point3D) that;
return super.equals(p) && this.z == p.z;
}
return false;
}
}
测试:
Point p1 = new Point(1, 1);
Point p2 = new Point3D(1, 1, 1);
System.out.println(p1.equals(p2)); // 显示 true
System.out.println(p2.equals(p1)); // 显示 false
这显然不满足对称性原则
修改如下:
public class Point3D extends Point {
public final int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point3D) {
Point3D p = (Point3D) that;
return super.equals(p) && this.z == p.z;
}
if(that instanceof Point) {
return that.equals(this);
}
return false;
}
}
继续测试:
Point p1 = new Point(1, 1);
Point p2 = new Point3D(1, 1, 1);
Point p3 = new Point3D(1, 1, 2);
System.out.println(p2.equals(p1)); // 显示 true
System.out.println(p1.equals(p3)); // 显示 true
System.out.println(p2.equals(p3)); // 显示 false
这不满足传递性原则,问题在于2D的点没有z轴,一般来说,对于不同的类,我们视之为不同。
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point) {
Point p = (Point) that;
return this.getClass() == p.getClass() &&
this.x == p.x &&
this.y == p.y;
}
return false;
}
@Override
public int hashCode() {
return 41 * (41 + x) + y;
}
}
public class Point3D extends Point {
public final int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point3D) {
Point3D p = (Point3D) that;
return super.equals(p) && this.z == p.z;
}
return false;
}
}
这种情况下Point只能跟Point比较,Point3D只能跟Point3D比较。
有如下一种情况:
Point p1 = new Point(1, 1);
Point p2 = new Point(1, 1) {
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
};
Set<Point> pSet = new HashSet<Point>();
pSet.add(p1);
System.out.println(pSet.contains(p1)); // 显示 true
System.out.println(pSet.contains(p2)); // 显示 false
可以修改如下:
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point) {
Point p = (Point) that;
return p.canEquals(this) &&
this.x == p.x &&
this.y == p.y;
}
return false;
}
public boolean canEquals(Object that) {
return that instanceof Point;
}
@Override
public int hashCode() {
return 41 * (41 + x) + y;
}
}
public class Point3D extends Point {
public final int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
@Override
public boolean equals(Object that) {
if(that instanceof Point3D) {
Point3D p = (Point3D) that;
return p.canEquals(this) &&
super.equals(p) && this.z == p.z;
}
return false;
}
@Override
public boolean canEquals(Object that) {
return that instanceof Point3D;
}
@Override
public int hashCode() {
return 41 * super.hashCode() + z;
}
}
这里在比较的时候,p1.equals(p2)由于传进来的实例可以取得this,可以反过来测试p2是不是等于p1,对于有具体名称的类,不会与父类有相等的可能性,对于匿名类,由于直接继承了父类的equals方法,所以反过来测试也是true