简单的一小段代码,有看不懂的地方,求讲解

notlogin notlogin 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
重写方法

public class Test {

Test test;

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((test == null) ? 0 : test.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Test other = (Test) obj;
if (test == null) {
if (other.test != null)
return false;
} else if (!test.equals(other.test))
return false;
return true;
}

}


问:
if (obj == null)
return false;
这个有什么用?如果test是null,obj也是null不就是true了吗?(只是假设,这个时候用equals会异常)

Test other = (Test) obj;
if (test == null) {
if (other.test != null)
return false;
} else if (!test.equals(other.test))
return false;

这段...我不知道它是在比较什么...

各位见笑了,求逐行讲解.

另外重写hashcode的代码根本就看不懂.....
...全文

相关推荐

评论 22
收藏 已收藏
分享
返回顶部
notlogin
Java SE 62,621 成员 307,251 内容