根据作业,我必须创建一个名为“equals”的方法来执行以下操作:
The method compares the instance variables of the calling object with instance variables of the parameter object for equality and returns
true
if the dollars and the cents of the calling object are the same as the dollars and the cents of the parameter object. Otherwise, it returnsfalse
.
更新:我犯了一个错误,放错了内容,已更新。出现新错误:
Money.java:115: error: long cannot be dereferenced
if (dollars.equals(otherObject.dollars) &&
^
Money.java:116: error: long cannot be dereferenced
cents.equals(otherObject.cents))
^
2 errors
使用方法:
public boolean equals(Money otherObject)
{
boolean status;
if (dollars.equals(otherObject.dollars) &&
cents.equals(otherObject.cents))
return status = true;
else
return status = false;
}
最佳答案
dollars
和 cents
是原始 long
类型吗?
如果是的话,您需要使用 (dollars == otherObject.dollars) && (cents == otherObject.cents) 。您无法在基元上调用方法(例如 equals()
)。
关于java - 创建 equals 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22463171/