第八条 覆盖equals是要遵守通用的约定

本文探讨了在Java中正确实现equals方法的重要性,强调了自反性、对称性、传递性和一致性四大基本原则,并通过实例展示了如何避免常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第八条 覆盖equals是要遵守通用的约定
覆盖equals必须满足以下四个要求:
[color=red](1)自反性[/color]
对于任何非null的引用值x,x.equals(x)返回true
[color=red](2)对称性[/color]
对于任何非null的引用值x和y,当且仅当y.equals(x)返回true时,

x.equals(y)必须返回true
[color=red](3)传递性[/color]
对于任何非null的引用值x、y和z,如果x.equals(y)返回true,

y.equals(z)返回true,则x.equals(z)返回true
[color=red](4)一致性[/color]
对于任何非null的引用值x和y,只要equals的比较操作在对象中所用的

信息没有被修改,多次调用x.equals(y)就会一致的返回ture或者false

(1)自反性是最为基本的性质,所以最好完成。
(2)对称性


public class MyString {

private final String s;

public MyString(String s) {
this.s = s;
}

@Override
public boolean equals(Object o) {
if(o instanceof MyString) {
MyString cis = (MyString)o;
return s.equalsIgnoreCase(cis.s);
}
if(o instanceof String) {
return s.equalsIgnoreCase((String)o);
}
return false;
}
}



import junit.framework.Assert;

import org.junit.Test;

public class TestMyString {

@Test
public void testEquals() {
MyString ms = new MyString("P");
String s = "p";
Assert.assertEquals(ms.equals(s), true);
Assert.assertEquals(s.equals(ms), false);
}

}

说明[color=darkred]自反性是被违背了[/color],所以可以将代码改为:

public class MyString {

private final String s;

public MyString(String s) {
this.s = s;
}

@Override
public boolean equals(Object o) {
if(o instanceof MyString) {
MyString cis = (MyString)o;
return s.equalsIgnoreCase(cis.s);
}
return false;
}
}

自己就和自己比较,不要贪多。
(3)传递性


public class MyPoint {

private final int x;
private final int y;

public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object o) {
if(!(o instanceof MyPoint))
return false;
MyPoint p = (MyPoint)o;
return p.x == x && p.y == y;
}
}



import java.awt.Color;


public class MyColorPoint extends MyPoint{

private Color c;

public MyColorPoint(int x, int y, Color c) {
super(x, y);
this.c = c;
}
@Override
public boolean equals(Object o) {
if(!(o instanceof MyColorPoint))
return o.equals(this);
if(!(o instanceof MyPoint))
return false;
return super.equals(o) && ((MyColorPoint)o).c

== c;
}
}



import java.awt.Color;

import org.junit.Assert;
import org.junit.Test;


public class TestMyColorPoint {

@Test
public void testEquals() {
MyPoint p = new MyPoint(1,2);
MyColorPoint mcp1 = new MyColorPoint

(1,2,Color.BLUE);
MyColorPoint mcp2 = new MyColorPoint

(1,2,Color.RED);
Assert.assertEquals(mcp1.equals(p), true);
Assert.assertEquals(p.equals(mcp2), true);
}
}

现在满足了对称性,又丧失了传递性,所以还要修改equals方法。
[size=large]
总之,equals和人的思维应该是一样的,所以全面的思考可能出现的各种情况,并且根据情况写出解决方法是写好equals的关键[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值