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()); } } 

你的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方法进行第一次检查。 但我会告诉你我是如何做我的平等方法的

 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个案例。

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

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

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

如果你重写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); }