-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEqualExample2.java
More file actions
51 lines (47 loc) · 1.32 KB
/
EqualExample2.java
File metadata and controls
51 lines (47 loc) · 1.32 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package code_03_Object;
/**
* 重写equals()方法
- 检查是否为同一个对象的引用,如果是直接返回 true;
- 检查是否是同一个类型,如果不是,直接返回 false;
- 将 Object 对象进行转型;
- 判断每个关键域是否相等。
*/
public class EqualExample2 {
private int x;
private int y;
private int z;
public EqualExample2(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + x;
result = 31 * result + y;
result = 31 * result + z;
return result;
}
@Override
public boolean equals(Object obj) {
// 检查是否为同一个对象的引用,如果是直接返回 true
if(this==obj){
return true;
}
//检查是否是同一个类型,如果不是,直接返回 false;
if(obj==null || obj.getClass()!=this.getClass()){
return false;
}
//将 Object 对象进行转型
EqualExample2 that=(EqualExample2)obj;
//判断每个关键域是否相等。
if(that.x!=this.x){
return false;
}
if(this.y!=this.y){
return false;
}
return that.z==this.z;
}
}