复写equals方法和hashcode方法

public boolean equals(Object obj) {

    1.首先比较传入的对象是否就是this对象:if(this==obj)

    2.判断obj是否是该类:if(obj instanceof 类名)

    3.如果是该类的子类,首先进行强制类型转换:类名 对象名=(类名)obj;

    4.继续根据实际的需要判断:  
       this.getId()==item.getId()&&this.getName().equals(item.getName())

}

还可参考如下博客:

Java:复写equals实例:https://www.cnblogs.com/tinyphp/p/3771989.html

java 序列化和 hashcode、equals 方法重写:https://blog.csdn.net/tengdazhang770960436/article/details/53436334

猜你喜欢

转载自blog.csdn.net/pbyang_love/article/details/81613294
今日推荐