您的位置:首页 > 编程语言 > Java开发

java equals

2015-12-29 00:11 369 查看
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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: