Java equals方法-如何重写
我需要有关重写Java equals方法-如何重写,java,methods,equals,Java,Methods,Equals,我需要有关重写equals方法的帮助。除了equals方法外,我所有的东西都在工作。我目前使用的equals方法没有给出正确答案。我似乎想不出是什么问题 我的班级: package myclasses; public class Currency { private int dollars, cents; public Currency() { dollars = 0; cents = 0; } public Curr
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());
}
}
package-myclass;
公营货币
{
私人整数美元,美分;
公共货币()
{
美元=0;
美分=0;
}
公共货币(整数d、整数c)
{
这是美元=d;
这0.5美分=c;
美分(美分);
}
公共货币(美元)
{
归还美元;
}
公共int getCents()
{
退分;
}
私人美元(整数美元)
{
美元=美元;
}
私有无效设置分(整数分)
{
而(美分>99)
{
美分=(美分-100);
美元++;
}
这1.5美分=美分;
}
公共无效设置金额(整数新美元,整数新美分)
{
美元;
美分(美分);
}
公共无效添加(整数美元,整数美分)
{
this.dollars=dollars+getDollars();
美分=美分+美分();
美分(美分);
}
公共布尔等于(对象美元,对象美分)
{
如果(此==美元&&此==美分)
返回true;
如果(!(美元货币实例)| |!(美分货币实例))
返回false;
货币=美元;
货币便士=(货币)美分;
返回(this.dollars==money.dollars)和&(this.cents==penny.cents);
//返回货币。美元。等于(货币。美分);
//返回此.equals(美元)和&this.equals(美分);
}
公共布尔值为零()
{
如果(getUSD()==0&&getCents()==0)
{
返回true;
}
返回false;
}
公共字符串toString()
{
返回“$”+getUSD()+”+
(getCents()<10?(“0”+getCents()):getCents());
}
}
您的equals()
方法有一些错误,如:
if(this == dollars && this == cents)
这永远不会是真的。。。这必须是:
if(this.dollars == dollars && this.cents == cents)
但我不会在编码equals上下功夫,建议自动生成equals。大概是这样的:
@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评论的那样,几乎不可避免)当您重写方法时,也重写hashCode()
在定义上: 请注意,每当重写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,以及它们是否来自同一类。请注意,它们是else if,因为不止这3种情况
如果尚未输入3条初始条件语句中的任何一条,则需要将obj参数强制转换到所处的类中。由于最后一个原因,您可以安全地这样做,如果-
else if (getClass() != obj.getClass()) {
return false;
}
在此之后,只需定义确定两个对象是否相同的规则。在我使用的示例中,我检查了类的两个字段的内容。如果它们相同,则对象是相等的。如果要重写equals方法,则上述代码未正确重写equals方法 请使用下面的代码来替代等于--
您确实意识到,
equals
通常用于比较另一个类似对象(即它的签名应该是equals(object other)
),它只有一个参数它不仅是“高度推荐的”,而且几乎不可避免地重写equals()和hashCode。请参阅equals的约定()@AdriaanKoster我相信Jordi已经知道了这一点,但可以在他的答案中添加该链接。我还建议使用谷歌库来避免编写类似于equals
、hashcode
和toString
的内容,或者避免在课堂上使用这些内容。谢谢@AdriaanKoster,添加了更多信息以澄清;)嗨,AVI,因为这是你的第一个问题,而不是如果这个或任何答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。当然,没有义务这样做。
public boolean equals(Object currency) {
Currency newref = null;
if (currency instanceof Currency) {
newref = (Currency)currency;
}
return (this.dollars == newref.dollars) && (this.cents == newref.cents);
}