英文:
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论