gpt4 book ai didi

java - 传递关系 : What is x. equals(z) 当 x.equals(y) 为假且 y.equals(z) 为真时

转载 作者:行者123 更新时间:2023-12-04 17:17:59 29 4
gpt4 key购买 nike

假设 equals() 是可传递的;我知道如果 x 和 y 有平等的双边协议(protocol),那么其中一个,比如 y,不会单独与第三类 z 签订协议(protocol)。 但是如果我们遇到 x.equals(y) = false(仍可传递)的情况,那么与 z 的双边协议(protocol)应该是什么?

最佳答案

如果 equals 方法被正确实现,如 Object 的 javadoc 所要求的:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

我们可以推断 x.equals(z) 一定是假的。

证明,如果 equals() 是可传递的和对称的,则 x.equals(y) 为假且 y.equals(z) 为真:

1) 假设 x.equals(z) 为真;
2) z.equals(y) 为真(对称);
1+2) x.equals(y) 为真(传递1和2)

但是x.equals(y)给定为false,所以number 1或number 2一定是错误的,即x.equals(z)为false或函数不对称。

但是如果 equals() 没有被实现为对称的,你就不能对结果 x.equals(z) 说任何话(见其他答案;我对@Stephen C 回答的评论)

关于java - 传递关系 : What is x. equals(z) 当 x.equals(y) 为假且 y.equals(z) 为真时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3554914/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com