且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于平等的***做法:过载还是不过载?

更新时间:2023-02-16 21:23:07

我不会看到重载equals的情况,除了更容易出错且更难维护,尤其是在使用继承时。

I'dont see the case for overloading equals, except that is more error-prone and harder to maintain, especially when using inheritance.

在这里,保持反身性,对称性和传递性或检测它们的不一致性是极其困难的,因为你总是必须知道被调用的实际equals方法。想想一个大的继承层次结构,只有一些类型实现自己的重载方法。

Here, it can be extremly hard to maintain reflexivity, symmetry and transitivity or to detect their inconsistencies, because you always must be aware of the actual equals method that gets invoked. Just think of a large inheritance hierarchie and only some of the types implementing their own overloading method.

所以我说不要这样做。