equals深入理解

package cn.galc.test;

public class TestEquals {
public static void main(String[] args) {
/**
* 这里使用构造方法Cat()在堆内存里面new出了两只猫,
* 这两只猫的color,weight,height都是一样的,
* 但c1和c2却永远不会相等,这是因为c1和c2分别为堆内存里面两只猫的引用对象,
* 里面装着可以找到这两只猫的地址,但由于两只猫在堆内存里面存储在两个不同的空间里面,
* 所以c1和c2分别装着不同的地址,因此c1和c2永远不会相等。
*/
Cat c1 = new Cat(1, 1, 1);
Cat c2 = new Cat(1, 1, 1);
System.out.println("c1==c2的结果是:"+(c1==c2));//false
System.out.println("c1.equals(c2)的结果是:"+c1.equals(c2));//false
}
}

class Cat {
int color, weight, height;

public Cat(int color, int weight, int height) {
this.color = color;
this.weight = weight;
this.height = height;
}
}
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
画出内存分析图分析c1和c2比较的结果,当执行Cat c1 = new Cat(1,1,1); Cat c2 = new Cat(1,1,1); 之后内存之中布局如下图:
         

由此我们看出,当我们new一个对象时,将在内存里加载一份它自己的内存,而不是共用!对于static修饰的变量和方法则保存在方法区中,只加载一次,不会再多copy一份内存。
所以我们在判断俩个对象逻辑上是否相等,即对象的内容是否相等不能直接使用继承于Object类的equals()方法,我们必须得重写equals()方法,改变这个方法默认的实现。下面在Cat类里面重写这个继承下来的equals()方法:
class Cat {
int color, weight, height;

public Cat(int color, int weight, int height) {
this.color = color;
this.weight = weight;
this.height = height;
}

/**
* 这里是重写相等从Object类继承下来的equals()方法,改变这个方法默认的实现,
* 通过我们自己定义的实现来判断决定两个对象在逻辑上是否相等。
* 这里我们定义如果两只猫的color,weight,height都相同,
* 那么我们就认为这两只猫在逻辑上是一模一样的,即这两只猫是“相等”的。
*/
public boolean equals(Object obj){
if (obj==null){
return false;
}
else{
/**
* instanceof是对象运算符。
* 对象运算符用来测定一个对象是否属于某个指定类或指定的子类的实例。
* 如果左边的对象是右边的类创建的对象,则运算结果为true,否则为false。
*/
if (obj instanceof Cat){
Cat c = (Cat)obj;
if (c.color==this.color && c.weight==this.weight && c.height==this.height){
return true;
}
}
}
return false;
}
}
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
设计思路很简单:先判断比较对象是否为null—>判断比较对象是否为要比较类的实例—–>比较俩个成员变量是否完全相等。
//另外一种常用重写方法
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass(http://www.amjmh.com/v/BIBRGZ_558768/)) return false;
People other = (People) obj;
if (age != other.age) return false;
if (firstName == null) {
if (other.firstName != null) return false;
} else if (!firstName.equals(other.firstName)) return false;
if (lastName == null) {
if (other.lastName != null) return false;
} else if (!lastName.equals(other.lastName)) return false;
return true;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这样通过在类中重写equals()方法,我们可以比较在同一个类下不同对象是否相等了。
---------------------

猜你喜欢

转载自www.cnblogs.com/ly570/p/11331584.html