简体   繁体   English

是否必须为最终类重写equals()和hashCode()方法

[英]Is it compulsory to override equals() and hashCode() methods for final classes

Is it compulsory to override the methods equals() and hashCode() for final classes? 是否必须为最终类重写equals()hashCode()方法?

Note : as all Wrapper classes are final and immutable and overrides the equals and hashCode methods. 注意:由于所有Wrapper类都是final和不可变的,并且覆盖equals和hashCode方法。

Thanks in advance 提前致谢

is it compulsary to override equals and hashCode methods for classes which are defined as final? 是否必须为定义为final的类重写equals和hashCode方法?

Define a class as final and overriding equals() and hashCode() are two different things. 将类定义为final并且重载equals()hashCode()是两件事。

Final is there to restrict a class not to be sub classed. Final限制了一个类不能被子类化。 And, equals() is there to check the equality of the instances of a class. 并且, equals()用于检查类实例的相等性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM