当前位置:网站首页>Overriding equals() & hashCode() in sub classes … considering super fields
Overriding equals() & hashCode() in sub classes … considering super fields
2022-07-05 22:16:00 【Superior virtue and weak water】
problem :
Is there a specific rule on how Overriding equals()
& hashCode()
in sub classes considering super fields ?? in consideration of Super field , Is there a question about how to In subclass rewrite equals()
and hashCode()
Specific rules for ?knowing that there is many parameters : super fields are private/public , with/without getter ... I know there are many parameters : Super fields are private / Public , Yes / nothing getter ...
For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and for example ,Netbeans Generated equals()&hashCode() Super fields will not be considered ... and
new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals( new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot"))
will return true :( Will return true :(
public class Hominidae { public String gender; public String weight; public String height; public Hominidae(String gender, String weight, String height) { this.gender = gender; this.weight = weight; this.height = height; } ... }public class HomoSapiens extends Hominidae { public String name; public String faceBookNickname; public HomoSapiens(String gender, String weight, String height, String name, String facebookId) { super(gender, weight, height); this.name = name; this.faceBookNickname = facebookId; } ... }
If you want to see the Netbeans generated equals() & hashCode() : If you want to see Netbeans Generated equals() and hashCode():
public class Hominidae { ... @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Hominidae other = (Hominidae) obj; if ((this.gender == null) ? (other.gender != null) : !this.gender.equals(other.gender)) { return false; } if ((this.weight == null) ? (other.weight != null) : !this.weight.equals(other.weight)) { return false; } if ((this.height == null) ? (other.height != null) : !this.height.equals(other.height)) { return false; } return true; } @Override public int hashCode() { int hash = 5; hash = 37 * hash + (this.gender != null ? this.gender.hashCode() : 0); hash = 37 * hash + (this.weight != null ? this.weight.hashCode() : 0); hash = 37 * hash + (this.height != null ? this.height.hashCode() : 0); return hash; }}public class HomoSapiens extends Hominidae { ... @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final HomoSapiens other = (HomoSapiens) obj; if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) { return false; } if ((this.faceBookNickname == null) ? (other.faceBookNickname != null) : !this.faceBookNickname.equals(other.faceBookNickname)) { return false; } return true; } @Override public int hashCode() { int hash = 7; hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0); hash = 89 * hash + (this.faceBookNickname != null ? this.faceBookNickname.hashCode() : 0); return hash; }}
Solution :
Reference resources : https://stackoom.com/en/question/8fhN边栏推荐
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
- Basic grammar of interview (Part 1)
- Leetcode simple question: check whether each row and column contain all integers
- Platformio create libopencm3 + FreeRTOS project
- A trip to Suzhou during the Dragon Boat Festival holiday
- 每日刷题记录 (十四)
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- Multiplexing of Oracle control files
- The American Championship is about to start. Are you ready?
- Win11运行cmd提示“请求的操作需要提升”的解决方法
猜你喜欢
Learning of mall permission module
Database recovery strategy
Shell script, awk condition judgment and logic comparison &||
Nacos 的安装与服务的注册
Solutions for unexplained downtime of MySQL services
The real situation of programmers
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
ICMP introduction
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Summary of concurrency control
随机推荐
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
Oracle views the data size of a table
Type of fault
AD637使用笔记
The statistics of leetcode simple question is the public string that has appeared once
Blocking protocol for concurrency control
Leetcode simple question: check whether each row and column contain all integers
The new content of the text component can be added through the tag_ Config set foreground and background colors
Pl/sql basic syntax
K210 learning notes (IV) k210 runs multiple models at the same time
A substring with a length of three and different characters in the leetcode simple question
MySQL服务莫名宕机的解决方案
微服务链路风险分析
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
[agc009e] eternal average - conclusion, DP
Tips for using SecureCRT
Practice: fabric user certificate revocation operation process
Installation of VMware Workstation
The difference between MVVM and MVC
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析