Enforce “equals” in an interface

前端 未结 6 1213
醉酒成梦
醉酒成梦 2021-02-15 04:06

I have an interface and I want everyone who implements this interface to implements an overrridden \"equals\" method.

Is there a way to make sure that happens?

6条回答
  •  渐次进展
    2021-02-15 04:41

    I've experimented with writing the required contract for equals in the JavaDoc. But the requirement that hashCode is consistent with equals results in a very complex contract for hashCode. So I gave up and created abstract base class with a final implementation of the two methods instead.

提交回复
热议问题