繁体   English   中英

equals方法-如何覆盖

[英]equals method - how to override

我需要帮助来覆盖equals方法。 除了equals方法之外,我已完成所有工作。 我目前拥有的equals方法无法给我正确的答案。 我似乎无法弄清楚可能是什么问题。

我的课:

package myclasses;

public class Currency
{
    private int dollars, cents;

    public Currency()
    {
        dollars = 0;
        cents = 0;
    }

    public Currency(int d, int c)
    {
        this.dollars = d;
        this.cents = c;

        setCents(cents);
    }

    public int getDollars()
    {
        return dollars;
    }

    public int getCents()
    {
        return cents;
    }

    private void setDollars(int dollars)
    {
        this.dollars = dollars;
    }

    private void setCents(int cents)
    {       
        while(cents > 99)
        {
            cents = (cents - 100);
            dollars++;
        }

        this.cents = cents;
    }

    public void setAmount(int newDollars, int newCents)
    {
        setDollars(dollars);
        setCents(cents);
    }

    public void add(int dollars, int cents)
    {
        this.dollars =  dollars + getDollars();
        cents = cents + getCents();

        setCents(cents);
    }

    public boolean equals(Object dollars, Object cents)
    {
        if(this == dollars && this == cents)
            return true;

        if(!(dollars instanceof Currency) || !(cents instanceof Currency))
            return false;

        Currency money = (Currency) dollars;
        Currency penny = (Currency) cents;

        return (this.dollars == money.dollars) && (this.cents == penny.cents);
        //return Currency.dollars.equals(Currency.cents);
        //return this.equals(dollars) && this.equals(cents);

    }

    public boolean isZero()
    {
        if(getDollars() == 0 && getCents() == 0)
        {
            return true;
        }
        return false;
    }

    public String toString()
    {
        return "$" + getDollars() + "."   +
                (getCents() < 10 ? ("0" + getCents()) : getCents());
    }
}

您的equals()方法有一些错误,例如:

if(this == dollars && this == cents)

这将永远是不正确的...这必须是:

if(this.dollars == dollars && this.cents == cents)

但是我不会花任何精力编写等值代码,建议自动生成等值代码。 像这样:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Currency other = (Currency) obj;
    if (cents != other.cents)
        return false;
    if (dollars != other.dollars)
        return false;
    return true;
}

强烈建议 ,(当您重写equals()方法时,也不可避免地要使用@AdriaanKoster进行注释),同时也重写hashCode()
equals()定义中:

请注意,通常有必要在重写此方法时重写hashCode方法,以维护hashCode方法的常规协定,该协定规定相等的对象必须具有相等的哈希码。

哈希码:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + cents;
    result = prime * result + dollars;
    return result;
}

我不太确定为什么要在equals方法中进行第一次检查。 但我会告诉你我通常如何做equals方法

public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    } else if (obj == null) {
        return false;
    } else if (getClass() != obj.getClass()) {
        return false;
    }

    //a cast of object to the class you are using should be here
    if (this.someField.equals(castObject.someField)
            && this.otherField.equals(castObject.otherField)) {
        return true;
    }

    return false;
}

所以这就是正在发生的事情。 该方法的第一部分进行基本检查-测试的对象是否与参数相同,该对象是否为null,以及它们是否来自同一类。 请注意,它们是if,因为不止这3种情况。

如果您没有输入3个初始条件语句中的任何一个,则需要将obj参数转换为您所在的类。您可以这样做,因为最后一个-

else if (getClass() != obj.getClass()) {
    return false;
} 

之后,只需定义规则即可确定两个对象是否相同。 在我使用的示例中,我正在检查类的两个字段的内容。 如果它们相同,则对象相等。

如果您要覆盖equals方法,则您上面的代码未正确覆盖equals方法。

用下面的代码代替等于

public boolean equals(Object currency) {

Currency newref = null;

if (currency instanceof Currency) {
  newref = (Currency)currency;
}
return (this.dollars == newref.dollars) && (this.cents == newref.cents);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM