当对象 o 为 null 时,无法在 equals() 方法中 return false。我已经添加了我的 equals() 实现以及测试用例
Not able to return false in equals() method when object o is null. I have added my equals() implementation as well the test case
我的 equals() 方法:
例如,当我的对象为 null 时在第二个 if 语句中,我应该 return false 但由于某种原因我的代码无法这样做。有帮助吗?
public boolean equals(Prof o) {
boolean res = false;
if(this == o) {
res = true;
}
if(o == null || this.getClass() != o.getClass()) {
res = false;
}
Prof other = (Prof) o;
if(this.year == other.year) {
if(this.id.equals(other.id)) {
res = true;
}
}
else {
res = false;
}
return res;
}
测试用例:
public void test02_ProfEqualHash() {
Prof p1 = new Prof("John S Lee", "yu213", 5);
assertTrue(p1.equals(p1));
Prof p0 = null; // null
assertFalse(p1.equals(p0)); // my equals() implementation fails here
Date d = new Date();
String s = "Hello";
assertFalse(p1.equals(d));
assertFalse(p1.equals(s));
Prof p2 = new Prof("John L", "yu213", 5);
assertTrue(p1.equals(p2));
assertTrue(p1.hashCode() == p2.hashCode());
assertTrue(p2.equals(p1));
Prof p3 = new Prof("John S Lee", "yu203", 5);
assertFalse(p1.equals(p3));
//assertNotEquals(p1.hashCode(), p3.hashCode());
Prof p4 = new Prof("Tracy T", "yu053", 2);
assertFalse(p1.equals(p4));
//assertNotEquals(p1.hashCode(), p4.hashCode());
Prof p5 = new Prof("John S Lee", "yu213", 8);
assertFalse(p1.equals(p5));
//assertTrue(p1.hashCode() != p5.hashCode());
}
首先,为了正确覆盖Object
的equals()
,方法签名应该是:
public boolean equals(Object o) {
....
}
即使您的测试代码调用您的 equals()
方法,JDK 类 期望 Object
的 equals()
签名也不会。
此外,当您发现 o
参数是 null
时,您应该立即 return false
,以免稍后在您的方法中访问它(这会导致 NullPointerException
).
正确的实现可能如下所示:
public boolean equals (Object o)
{
if (this == o) {
return true;
}
if (o == null || !(o instanceof Prof)) {
return false;
}
Prof other = (Prof) o;
return this.year == other.year && this.id.equals(other.id);
}
在Java中,对象class是每个class的超class。所以为了覆盖Objectclass中定义的equal方法,需要遵循相同的方法定义,即:
@Override
public boolean equals(Object other) {
// here goes your implementation class
}
由于您对 equals
的定义具有 Prof
作为参数,因此您实际上并没有覆盖对象 equals
方法。
有关equals
合同的更多信息,您可以阅读Josh Bloch
的Effective Java
书中的Item10
。
此外,如果您的 class 有一个 equals 方法,那么您也应该始终定义 hashCode 实现。下面是这个方法的实现:
@Override
public int hashCode() {
return Objects.hash(year, id);
}
我的 equals() 方法: 例如,当我的对象为 null 时在第二个 if 语句中,我应该 return false 但由于某种原因我的代码无法这样做。有帮助吗?
public boolean equals(Prof o) {
boolean res = false;
if(this == o) {
res = true;
}
if(o == null || this.getClass() != o.getClass()) {
res = false;
}
Prof other = (Prof) o;
if(this.year == other.year) {
if(this.id.equals(other.id)) {
res = true;
}
}
else {
res = false;
}
return res;
}
测试用例:
public void test02_ProfEqualHash() {
Prof p1 = new Prof("John S Lee", "yu213", 5);
assertTrue(p1.equals(p1));
Prof p0 = null; // null
assertFalse(p1.equals(p0)); // my equals() implementation fails here
Date d = new Date();
String s = "Hello";
assertFalse(p1.equals(d));
assertFalse(p1.equals(s));
Prof p2 = new Prof("John L", "yu213", 5);
assertTrue(p1.equals(p2));
assertTrue(p1.hashCode() == p2.hashCode());
assertTrue(p2.equals(p1));
Prof p3 = new Prof("John S Lee", "yu203", 5);
assertFalse(p1.equals(p3));
//assertNotEquals(p1.hashCode(), p3.hashCode());
Prof p4 = new Prof("Tracy T", "yu053", 2);
assertFalse(p1.equals(p4));
//assertNotEquals(p1.hashCode(), p4.hashCode());
Prof p5 = new Prof("John S Lee", "yu213", 8);
assertFalse(p1.equals(p5));
//assertTrue(p1.hashCode() != p5.hashCode());
}
首先,为了正确覆盖Object
的equals()
,方法签名应该是:
public boolean equals(Object o) {
....
}
即使您的测试代码调用您的 equals()
方法,JDK 类 期望 Object
的 equals()
签名也不会。
此外,当您发现 o
参数是 null
时,您应该立即 return false
,以免稍后在您的方法中访问它(这会导致 NullPointerException
).
正确的实现可能如下所示:
public boolean equals (Object o)
{
if (this == o) {
return true;
}
if (o == null || !(o instanceof Prof)) {
return false;
}
Prof other = (Prof) o;
return this.year == other.year && this.id.equals(other.id);
}
在Java中,对象class是每个class的超class。所以为了覆盖Objectclass中定义的equal方法,需要遵循相同的方法定义,即:
@Override
public boolean equals(Object other) {
// here goes your implementation class
}
由于您对 equals
的定义具有 Prof
作为参数,因此您实际上并没有覆盖对象 equals
方法。
有关equals
合同的更多信息,您可以阅读Josh Bloch
的Effective Java
书中的Item10
。
此外,如果您的 class 有一个 equals 方法,那么您也应该始终定义 hashCode 实现。下面是这个方法的实现:
@Override
public int hashCode() {
return Objects.hash(year, id);
}