equals方法-如何覆盖 [英] equals method - how to override

查看:105
本文介绍了equals方法-如何覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来覆盖equals方法.除了equals方法,我所有的东西都在工作.我目前使用的equals方法没有给我正确的答案.我似乎无法弄清楚可能是什么问题.

I need help on to override the equals method. I have everything working except for the equals method. The equals method that I currently have is not giving me the correct answer. I can not seem to figure out what could be the problem.

我的课:

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()方法有一些错误,例如:

Your equals() method has some errors like:

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

这将永远不是真的...这必须是:

This will never be true... this must be:

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

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

But I won't put any effort in coding the equals, is recommended to autogenerate equals. Something like this:

@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;
}

也强烈建议您使用(强烈建议使用@AdriaanKoster进行评论)(当您覆盖 equals() 定义:

Also is highly recommended, (nearly unavoidable as @AdriaanKoster commented) when you override equals() method, also override hashCode()
In equals() definition:

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

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

哈希码:

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

这篇关于equals方法-如何覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆