1.重写equels方法
按照样例代码,重写equels方法
@Data
public class PositionDatas {
private Position red;
private Position blue;
private Position box;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
PositionDatas other = (PositionDatas) obj; //PositionDatas 为类名
//前四行代码为通用代码,后面的代码因类中元素不同而有区别
if(this.getRed().getX()!=other.getRed().getX()) return false;
if(this.getRed().getY()!=other.getRed().getY()) return false;
if(this.getBlue().getX()!=other.getBlue().getX()) return false;
if(this.getBlue().getY()!=other.getBlue().getY()) return false;
if(this.getBox().getX()!=other.getBox().getX()) return false;
if(this.getBox().getY()!=other.getBox().getY()) return false;
return true;
}
}
2.调用代码
调用代码时,请遵照最新阿里java开发公约,使用Objects.equals
if (!Objects.equals(a,b)) { //a,b为上述类的实例
}
-------------------------------------
尊敬的开发者,您好!我是一名初入职场的小小程序员,从事软件开发工作,定期发一些文章来记录自己的学习过程,并分享问题的解决方法。
如果样例代码和文章描述有哪些问题,或者有相关领域想一起讨论,欢迎私信交流。