kotlin 结构相等性是否检查超类型?

Does kotlin structural equality check super types?

我仔细阅读了我的主题标题,我只是想确保我理解正确。似乎答案是我们仍然需要覆盖 class 上的 equals 来定义结构相等性检查是什么?所以换句话说,如果我们想检查 customer1 == customer2 那么我们首先必须通过实现 equals 来定义 Customer 的结构相等性,此时 Kotlin 将通过 [= 使用我们的实现12=] 运算符?

所以在这种情况下,如果 Customer 继承自 Person 并且我们还想使用 Person 的属性执行结构相等性检查,那么我们将在 equals(Object object) 方法?

So in other words if we want to check the customer1 == customer2 then we first have to define what structural equality is for Customer by implementing equals, and at that point Kotlin will use our implementation by means of the == operator?

是的。如果 Customerdata class,那么你会自动获得 equals 的实现,否则你需要显式定义它。

So in this case if Customer inherits from Person and we also want perform a structural equality check using properties from Person then we would implement that in the equals(Object object) method?

同样,是的(使用 Any? 而不是 Object),就像您在 Java 中所做的那样,而且它...很复杂。有关更多信息,请参阅 and 以及从中链接的文章。

我要补充一点,Kotlin 对术语 "structural equality" 的用法并不正常。它通常保留用于通过比较所有(相关)字段的相等性来检查相等性的情况,例如 data class,而不是 "whatever equals happens to do".