我的 equals() 方法:例如,当我的对象为 null 时,在第二个 if 语句中,我应该返回 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;
}
Run Code Online (Sandbox Code Playgroud)
测试用例:
public void test02_ProfEqualHash() {
Prof p1 = new Prof("John S Lee", "yu213", 5);
assertTrue(p1.equals(p1));
Prof p0 …Run Code Online (Sandbox Code Playgroud)