如何根据属性比较来自同一类的对象?

我有这个对象:


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

另外,我实现了 HashCode 并等于:


@Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result + ((NumeroParte == null) ? 0 : NumeroParte.hashCode());

        result = prime * result + ((descripcion == null) ? 0 : descripcion.hashCode());

        long temp;

        temp = Double.doubleToLongBits(monto);

        result = prime * result + (int) (temp ^ (temp >>> 32));

        result = prime * result + referencia;

        return result;

    }

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        COSTOS other = (COSTOS) obj;

        if (NumeroParte == null) {

            if (other.NumeroParte != null)

                return false;

        } else if (!NumeroParte.equals(other.NumeroParte))

            return false;

        if (descripcion == null) {

            if (other.descripcion != null)

                return false;

        } else if (!descripcion.equals(other.descripcion))

            return false;

        if (Double.doubleToLongBits(monto) != Double.doubleToLongBits(other.monto))

            return false;

        if (referencia != other.referencia)

            return false;

        return true;

    }

我如何实现一种可以打印所有不等于的属性的方法?


我尝试使用“import java.util.Objects;” 使用:“Objects.hash(referencia, monto, descripcion, NumeroParte);”,这样可以给我打印的结果


一只萌萌小番薯
浏览 205回答 2
2回答

森栏

如果我正确理解您的要求,您想打印出两个对象中不相同的属性值,那么您可以创建如下方法。public void compareAttributes(COSTOS other) {&nbsp; &nbsp; if (this.getMonto() != other.getMonto()) {&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Not equal. This obj : " + this.getMonto()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " Other obj : " + other.getMonto());&nbsp; &nbsp; }&nbsp; &nbsp; // you can do the same for the remaining attributes.}编辑:正如@Andreas 在评论中指出的,您应该将此方法放在您的COSTOS类本身中,以便可以轻松比较每个对象。

芜湖不芜

首先,可以使用ObjectsJava 7 中添加的空安全辅助方法来简化您的方法:@Overridepublic int hashCode() {&nbsp; &nbsp; final int prime = 31;&nbsp; &nbsp; int result = 1;&nbsp; &nbsp; result = prime * result + Objects.hashCode(this.NumeroParte);&nbsp; &nbsp; result = prime * result + Objects.hashCode(this.descripcion);&nbsp; &nbsp; result = prime * result + Double.hashCode(this.monto);&nbsp; &nbsp; result = prime * result + this.referencia;&nbsp; &nbsp; return result;}@Overridepublic boolean equals(Object obj) {&nbsp; &nbsp; if (this == obj)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; if (obj == null || getClass() != obj.getClass())&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; COSTOS other = (COSTOS) obj;&nbsp; &nbsp; return (Objects.equals(this.NumeroParte, other.NumeroParte)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& Objects.equals(this.descripcion, other.descripcion)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& Double.doubleToLongBits(this.monto) == Double.doubleToLongBits(other.monto)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& this.referencia == other.referencia);}我如何实现一种可以打印所有不等于的属性的方法?要打印差异,请执行与equals方法相同的比较:public void printDifferences(COSTOS other) {&nbsp; &nbsp; if (! Objects.equals(this.NumeroParte, other.NumeroParte))&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Different NumeroParte: " + this.NumeroParte + " != " + other.NumeroParte);&nbsp; &nbsp; if (! Objects.equals(this.descripcion, other.descripcion))&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Different descripcion: " + this.descripcion + " != " + other.descripcion);&nbsp; &nbsp; if (Double.doubleToLongBits(this.monto) != Double.doubleToLongBits(other.monto))&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Different monto: " + this.monto + " != " + other.monto);&nbsp; &nbsp; if (this.referencia != other.referencia)&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Different referencia: " + this.referencia + " != " + other.referencia);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java