equals方法 - 如何覆盖

时间:2015-06-26 07:17:41

标签: java methods equals

我需要帮助来覆盖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());
    }
}

3 个答案:

答案 0 :(得分:7)

您的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;
}

答案 1 :(得分:1)

我不太清楚你为什么要用你的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,以及它们是否来自同一个类。请注意,如果不是这样的话,那么它们就是其他因素。

如果您没有输入3个初始条件语句中的任何一个,则需要将obj参数强制转换为您所在的类。由于最后一个if,您可以安全地执行此操作。

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

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

答案 2 :(得分:-1)

如果要覆盖equals方法,那么上面的代码没有正确覆盖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);
}
相关问题