当前位置:网站首页>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边栏推荐
- Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
- Storage optimization of performance tuning methodology
- 微服務鏈路風險分析
- Sentinel production environment practice (I)
- C language knowledge points link
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- 科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
- Comment développer un plug - in d'applet
- Summary of El and JSTL precautions
- Shell script, awk uses if, for process control
猜你喜欢
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
CA certificate trampled pit
What changes has Web3 brought to the Internet?
What about data leakage? " Watson k'7 moves to eliminate security threats
Overview of database recovery
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Oracle hint understanding
科技云报道:算力网络,还需跨越几道坎?
Stored procedures and stored functions
Promql demo service
随机推荐
Pinctrl subsystem and GPIO subsystem
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Some tutorials install the database on ubantu so as not to occupy computer memory?
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Leetcode simple question ring and rod
Poj3414 extensive search
Overview of database recovery
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
Damn, window in ie open()
CA certificate trampled pit
每日刷题记录 (十四)
Text组件新增内容通过tag_config设置前景色、背景色
Pl/sql basic syntax
Talking about MySQL index
Tips for using SecureCRT
Codeforces 12D ball tree array simulation 3 sorting elements
Platform bus
Shell script, awk condition judgment and logic comparison &||
A substring with a length of three and different characters in the leetcode simple question
MySQL actual combat 45 lecture learning (I)