Java 如何基于属性比较同一类中的对象?
我有这样的目标:Java 如何基于属性比较同一类中的对象?,java,equals,hashcode,Java,Equals,Hashcode,我有这样的目标: COSTOS Costos = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ"); COSTOS Herramienta = new COSTOS(1795, 299.11, "BISAG.INF.PUER.TRA.I", "67960T2MT02ZZ"); 这是我的班级: public class COSTOS implements Comparable<COSTOS&g
COSTOS Costos = new COSTOS(1781, 359.13, "BISAG.SUP.PUER.TRA.I", "67550T9AT00ZZ");
COSTOS Herramienta = new COSTOS(1795, 299.11, "BISAG.INF.PUER.TRA.I", "67960T2MT02ZZ");
这是我的班级:
public class COSTOS implements Comparable<COSTOS>{
public int referencia;
public double monto;
public String descripcion;
public String NumeroParte;
//Constructor
//getters setters
如何实现可以打印所有属性的方法
这不平等吗
我尝试使用“import java.util.Objects;”来使用:“Objects.hash(referencea,monto,description,numerioparte);”,这样可能会给我打印结果如果我正确理解您的需求,您希望打印出两个对象中不相同的属性值,那么您可以创建如下方法
public void compareAttributes(COSTOS other) {
if (this.getMonto() != other.getMonto()) {
System.out.println("Not equal. This obj : " + this.getMonto()
+ " Other obj : " + other.getMonto());
}
// you can do the same for the remaining attributes.
}
编辑:
正如@Andreas在评论中指出的,您应该将此方法放在
COSTOS
类本身中,以便可以轻松比较每个对象。首先,可以使用Java 7中添加的空安全帮助器方法简化方法:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode(this.NumeroParte);
result = prime * result + Objects.hashCode(this.descripcion);
result = prime * result + Double.hashCode(this.monto);
result = prime * result + this.referencia;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
COSTOS other = (COSTOS) obj;
return (Objects.equals(this.NumeroParte, other.NumeroParte)
&& Objects.equals(this.descripcion, other.descripcion)
&& Double.doubleToLongBits(this.monto) == Double.doubleToLongBits(other.monto)
&& this.referencia == other.referencia);
}
如何实现一个可以打印所有不相等属性的方法
要打印差异,请执行与equals
方法相同的比较:
public void printDifferences(COSTOS other) {
if (! Objects.equals(this.NumeroParte, other.NumeroParte))
System.out.println("Different NumeroParte: " + this.NumeroParte + " != " + other.NumeroParte);
if (! Objects.equals(this.descripcion, other.descripcion))
System.out.println("Different descripcion: " + this.descripcion + " != " + other.descripcion);
if (Double.doubleToLongBits(this.monto) != Double.doubleToLongBits(other.monto))
System.out.println("Different monto: " + this.monto + " != " + other.monto);
if (this.referencia != other.referencia)
System.out.println("Different referencia: " + this.referencia + " != " + other.referencia);
}
你能确切地说明你在哪里遇到困难吗?这应该包括一个不能像你期望的那样工作的代码示例(解释原因),或者一个你不能产生所述代码的特定点。好的。。。我只是想用HashCode和equals比较这些对象,但我需要打印不等于的属性。。。这就是我所有的代码,我想知道是否有人能帮我打印这些元素。你想打印出两个对象的属性不一样吗?是的,我怎么开始?“我如何实现一个方法来打印所有不相等的属性?”您编写的方法与
equals
方法非常相似,只是您打印了一些内容,而不是返回false
。要么将方法设为static
,要么删除参数obj1
,改用this
(在这种情况下,obj2
应重命名为other
,以保持一致性)。是,is应作为非静态1-arg方法添加到COSTOS
类中,类似于equals
和compareTo
。如果不是,则需要2-arg方法。2-arg方法应该是static
,无论是添加到COSTOS
类还是其他类。很好,但是如何从该方法打印?System.out.println(打印差异(COSTOS-other))@AntonioAlejos呃什么?你看过密码了吗?该方法称为printDifferences
(注意名称的打印部分),它执行System.out.println
。是的,我知道,但是我在我的类中实现了这个方法,当我运行我的项目时,它不会打印任何东西,因为我需要在我的主类中调用这个方法……你是说你不知道如何在Java中调用一个方法?也许您应该(重新)阅读Java指南,了解Java中方法的工作原理。要提供帮助,请尝试:c1。打印差异(c2)
其中c1
和c2
是类COSTOS
的实例好的,这对我很有用,非常感谢!!我正在学习这个,这对我来说有点新鲜。
public void printDifferences(COSTOS other) {
if (! Objects.equals(this.NumeroParte, other.NumeroParte))
System.out.println("Different NumeroParte: " + this.NumeroParte + " != " + other.NumeroParte);
if (! Objects.equals(this.descripcion, other.descripcion))
System.out.println("Different descripcion: " + this.descripcion + " != " + other.descripcion);
if (Double.doubleToLongBits(this.monto) != Double.doubleToLongBits(other.monto))
System.out.println("Different monto: " + this.monto + " != " + other.monto);
if (this.referencia != other.referencia)
System.out.println("Different referencia: " + this.referencia + " != " + other.referencia);
}