package com.cavaness.quartzbook.chapter3;
public class Point {
private int x;
private int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
public static void main(String[] args) {
Point point = new Point(1, 2);
Point point2 = new Point(1, 2);
System.out.println(point.equals(point2)); // 如果没有覆盖equals方法,默认的equals方式和==的功能是一样的,输出false,覆盖之后,输出true
}
}
覆盖equals
最新推荐文章于 2022-02-14 22:51:12 发布