gpt4 book ai didi

java - 比较具有大量属性的类的两个实例

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:29 24 4
gpt4 key购买 nike

我有一个类有超过 30 个属性。

我想覆盖 equals 方法以便比较我的类的两个实例。

但是我想避免像这样重写我的方法中的所有 30 个属性

@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Address address = (Address) o;
return Objects.equals(this.attr1, address.attr1) &&
Objects.equals(this.attr2, address.attr2) &&
Objects.equals(this.attr3, address.attr3) &&
......
Objects.equals(this.attr30, address.attr30);

}

你有更简单妥当的方法吗?

最佳答案

好吧,这基本上是必要的样板代码类型。幸运的是,有很多开发人员和您一样讨厌编写这样的代码。出于这样的原因, Lombok 项目成立了。

see this link.

例如,请参阅从我上面链接的页面中提取的以下两个代码片段:

Lombok

import lombok.EqualsAndHashCode;

@EqualsAndHashCode(exclude={"id", "shape"})
public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;

public String getName() {
return this.name;
}

@EqualsAndHashCode(callSuper=true)
public static class Square extends Shape {
private final int width, height;

public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}

普通 Java

import java.util.Arrays;

public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;

public String getName() {
return this.name;
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof EqualsAndHashCodeExample)) return false;
EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (Double.compare(this.score, other.score) != 0) return false;
if (!Arrays.deepEquals(this.tags, other.tags)) return false;
return true;
}

@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.score);
result = (result*PRIME) + (this.name == null ? 43 : this.name.hashCode());
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.tags);
return result;
}

protected boolean canEqual(Object other) {
return other instanceof EqualsAndHashCodeExample;
}

public static class Square extends Shape {
private final int width, height;

public Square(int width, int height) {
this.width = width;
this.height = height;
}

@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Square)) return false;
Square other = (Square) o;
if (!other.canEqual((Object)this)) return false;
if (!super.equals(o)) return false;
if (this.width != other.width) return false;
if (this.height != other.height) return false;
return true;
}

@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + super.hashCode();
result = (result*PRIME) + this.width;
result = (result*PRIME) + this.height;
return result;
}

protected boolean canEqual(Object other) {
return other instanceof Square;
}
}
}

如果您喜欢这种方法,我建议您完整地查看 Project Lombok。它确实有助于清理您的代码!

<强>!!!当心!!!为了能够实际使用 Lombok 生成的方法,您需要将 Lombok 插件安装到您的 IDE 中!否则,您的 IDE 将不知道自动生成的方法。

关于java - 比较具有大量属性的类的两个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49823340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com