Equals:
Object 类中的 equals 方法用于检测一个对象是否等于另一个对象。
实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class MyClass {
private Integer field1;
private String field2;
@Override public boolean equals(Object otherObject) {
if (this == otherObject) { return true; }
if (otherObject == null) { return false; }
if (!(otherObject instanceof MyClass)) { return false; }
MyClass other = (MyClass) otherObject; return this.field1.equals(other.field1) && this.field2.equals(other.field2); } }
|