第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據(jù)屬性比較來自同一類的對象?

如何根據(jù)屬性比較來自同一類的對象?

一只萌萌小番薯 2021-12-10 14:40:57
我有這個對象: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另外,我實現(xiàn)了 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;    }我如何實現(xiàn)一種可以打印所有不等于的屬性的方法?我嘗試使用“import java.util.Objects;” 使用:“Objects.hash(referencia, monto, descripcion, NumeroParte);”,這樣可以給我打印的結(jié)果
查看完整描述

2 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗 獲得超5個贊

如果我正確理解您的要求,您想打印出兩個對象中不相同的屬性值,那么您可以創(chuàng)建如下方法。


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 在評論中指出的,您應(yīng)該將此方法放在您的COSTOS類本身中,以便可以輕松比較每個對象。


查看完整回答
反對 回復(fù) 2021-12-10
?
蕪湖不蕪

TA貢獻(xiàn)1796條經(jīng)驗 獲得超7個贊

首先,可以使用ObjectsJava 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);

}

我如何實現(xiàn)一種可以打印所有不等于的屬性的方法?


要打印差異,請執(zhí)行與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);

}


查看完整回答
反對 回復(fù) 2021-12-10
  • 2 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號