实现单例模式的equals和hashcode方法?

huangapple 未分类评论46阅读模式
英文:

Implement equals and hashcode for singleton?

问题

我认为始终为每个类实现自定义的 equals(...)hashCode() 是一个不错的方法。但是对于一个单例模式类来说,实现这些方法是否有用呢?

你应该实现它们并且在 equals 方法中只返回 == 情况,对于哈希码则可以返回任意随机整数;或者你可以选择不实现它们,而是使用隐式的方法。什么是最佳做法呢?

英文:

I think it is a good way to always implement custom equals(...) and hashCode() for every class. But would is it useful to implement those methods for a singleton?

Should you implement them and just return the == case in equals method and any random int for the hash or just not implement them and use the implicit methods? What is the best practice?

huangapple
  • 本文由 发表于 2020年4月7日 18:17:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61077716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定