java equals 的用法
class animal {
int color;
int height, weight;
public animal(int color, int height, int weight) {
this.color = color;
this.height = height;
this.weight = weight;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}else {
if (obj instanceof animal) {
animal c = (animal)obj;
if (c.color == this.color && c.height == this.height && c.weight == this.weight) {
return true;
}
}
}
return false;
}
}
// equals 重写
public class TestEquals {
public static void main(String[] args) {
animal a1 = new animal(1,2,3);
animal a2 = new animal(1,2,3);
System.out.println(a1 == a2);
System.out.println(a1.equals(a2));
}
}