我是靠谱客的博主 贪玩发箍,这篇文章主要介绍覆盖Object类的equals、hashCode和toString方法,现在分享给大家,希望可以做个参考。

1.equals方法
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/** * 覆盖前equals和“==”比较的都是内存地址 * 覆盖equals方法,覆盖后的equals比较的是值,== 比较的是内存地址 */ public class CoverEquals{ public static void main(String[] args){ Equals e1 = new Equals(); Equals e2 = new Equals(); e1.id = 100; e2.id = 100; System.out.println(e1 == e2); System.out.println(e1.equals(e2)); } } class Equals{ public int id = 0; //作为对象属性值使用 //复写equals() public boolean equals(Object obj){ //形参 if(this == obj){ //第一步,判断是否是同一个实例 return true; } if(obj == null){ //第二步,判断要比较的对象是否为null return false; } if(obj instanceof Equals){ //第三步,判断是否是同一个类型 //Equals e = (Equals)obj; //第四步,类型相同,转换为同一个类型 if(this.id == obj.id){ //第五步,进行对象属性值的比较 return true; }else{ return false; } }else{ return false; //类型不同,直接返回false } } }

2.hashCode方法
hashCode是按照一定的算法得到的一个数值,是对象的散列码值。主要用来在集合中实现快速查找等操作。
Java规范里面规定,覆盖equals方法应该连带覆盖hashCode方法。
下面程序是采用一定算法来实现hashCode方法:

复制代码
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
/** * 覆盖equals时,应该连带覆盖hashCode方法 * 以下是eclipse自动生成的 */ import java.util.Arrays; public class HashCodeTest{ private byte byteValue; private char charValue; private short shortValue; private int intValue; private long longValue; private boolean booleanValue; private float floatValue; private double doubleValue; private String uuid; private int[] intArray = new int[3]; public int hashCode(){ final int prime = 31; int result = 1; result = prime * result + (booleanValue ? 1231 : 1237); result = prime * result + charValue; long temp; temp = Double.doubleToLongBits(doubleValue); result = prime * result + (int)(temp ^ (temp >>> 32)); result = prime * result + Float.floatToIntBits(floatValue); result = prime * result + Arrays.hashCode(intArray); result = prime * result + intValue; result = prime * result + (int)(longValue ^ (longValue >>> 32)); result = prime * result + shortValue; result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); return result; } }

3.toString方法也是Object类的方法,覆盖它可以让你得到更适用的返回信息。
代码示例如下:

复制代码
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
/** * 覆盖toString方法 */ public class CovertoString{ public static void main(String[] args){ MyInformation m = new MyInformation("dongshuai","18800000000",180); System.out.println(m); System.out.println(m.toString()); YourInformation y = new YourInformation("dongshuai","18800000000",180); System.out.println(y); } } class MyInformation{ private String name; private String phone; private int stature; public MyInformation(String name,String phone,int stature){ this.name = name; this.phone = phone; this.stature = stature; } } class YourInformation{ private String name; private String phone; private int stature; public YourInformation(String name,String phone,int stature){ this.name = name; this.phone = phone; this.stature = stature; } public String toString(){ return name + "<-->" + phone + "<-->" + stature; } }

最后

以上就是贪玩发箍最近收集整理的关于覆盖Object类的equals、hashCode和toString方法的全部内容,更多相关覆盖Object类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(99)

评论列表共有 0 条评论

立即
投稿
返回
顶部