java的area方法_为什么Java的Area#等于方法不覆盖Object#equals?

RealSkeptic链接到

JDK-4391558在上面的评论。该bug的

comment解释了推理:

The problem with overriding equals(Object) is that you must also

override hashCode() to return a value which guarantees that equals()

is true only if the hashcodes of the two objects are also equal.

但:

The problem here is that Area.equals(Area) does not perform a very

straight-forward comparison. It painstakingly examines each and every

piece of geometry in the two Areas and tests to see if they cover the

same enclosed spaces. Two Area objects could have a completely

different description of the same enclosed space and equals(Area)

would detect that they were the same.

所以基本上我们留下了一系列不太好的选择,比如:

deprecate equals(Area) and create an alternate name for that

operation, such as “areasEqual” so as to avoid the confusion.

Unfortunately, the old method would remain and would be linkable and

would trap many people who were intending to invoke the equals(Object)

version.

要么:

deprecate equals(Area) and change its implementation to be exactly

that of equals(Object) so as to avoid semantic problems if the wrong

method is called. Create a new method with a different name to avoid

confusion to implement the old functionality provided by equals(Area).

要么:

implement equals(Object) to call equals(Area) and implement a dummy

hashCode() which honors the equals/hashCode contract in a degenerate

way by returning a constant. This would make the hashCode method

essentially useless and make Area objects nearly useless as keys in a

HashMap or Hashtable.

或其他修改equals(Area)行为的方式来改变其语义或使其与hashCode不一致。

看起来,改变这种方法被维护者认为是不可行的(因为bug注释中概述的两个选项都没有解决这个问题)也不重要(因为方法执行的速度很慢,并且在比较时可能只会返回true一个区域的实例,就像评论者所说的那样)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值