第八条 覆盖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)对称性
说明[color=darkred]自反性是被违背了[/color],所以可以将代码改为:
自己就和自己比较,不要贪多。
(3)传递性
现在满足了对称性,又丧失了传递性,所以还要修改equals方法。
[size=large]
总之,equals和人的思维应该是一样的,所以全面的思考可能出现的各种情况,并且根据情况写出解决方法是写好equals的关键[/size]
覆盖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]