当前位置:网站首页>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边栏推荐
- Pl/sql basic case
- Comment développer un plug - in d'applet
- 笔记本电脑蓝牙怎么用来连接耳机
- Cross end solutions to improve development efficiency
- Hysbz 2243 staining (tree chain splitting)
- Poj3414 extensive search
- 微服务链路风险分析
- Damn, window in ie open()
- 2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
- How to develop and introduce applet plug-ins
猜你喜欢

Index optimization of performance tuning methodology

Bitbucket installation configuration

Calculation method of boundary IOU

Nacos 的安装与服务的注册

Practice: fabric user certificate revocation operation process

Damn, window in ie open()

Decorator learning 01

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

Meituan dynamic thread pool practice ideas, open source

如何快速体验OneOS
随机推荐
K210学习笔记(四) K210同时运行多个模型
"Chris Richardson microservices series" uses API gateway to build microservices
The new content of the text component can be added through the tag_ Config set foreground and background colors
Analyse des risques liés aux liaisons de microservices
Leetcode simple question: the minimum cost of buying candy at a discount
阿龙的感悟
The American Championship is about to start. Are you ready?
Decorator learning 01
Cobaltstrike builds an intranet tunnel
Leetcode simple question check whether all characters appear the same number of times
MySQL服务莫名宕机的解决方案
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Multiplexing of Oracle control files
如何向mongoDB中添加新的字段附代码(全)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Oracle advanced query
How to use tensorflow2 for cat and dog classification and recognition