英文:
Comparing objects and assigning the right value
问题
你好,我正在尝试比较两个支票账户对象的余额,但是遇到了一些问题。
public int compareTo(Object other) {
int otherBalance = 0;
other = (Bankable) other;
otherBalance = this.getBalance();
if (otherBalance > getBalance()) {
return otherBalance - getBalance();
} else if (otherBalance < getBalance()) {
return getBalance() - otherBalance;
} else
return 0;
}
上面的代码存在逻辑错误,即 otherBalance 等于错误对象的余额。这导致对该方法的调用始终返回 0。
我尝试通过设置 otherBalance = other.getBalance(); 来纠正这个错误,然而这会返回编译器错误 "cannot find symbol"。如果您能解释一下原因,我会非常感谢。
英文:
Hello I am trying to compare the balance of two checking account objects for my class but I've run into a few problems.
public int compareTo(Object other) {
int otherBalance = 0;
other = (Bankable) other;
otherBalance = this.getBalance();
if (otherBalance > getBalance()) {
return otherBalance - getBalance();
}
else if (otherBalance < getBalance()) {
return getBalance() - otherBalance;
}
else
return 0;
}
The code above has a logic error such that otherBalance is equal to the wrong object's balance. This results in calls to this method always returning 0.
I've tried to correct this mistake by setting otherBalance = other.getBalance(); however this returns the compiler error cannot find symbol. If you could explain why that is I would appreciate it.
答案1
得分: 1
这一行
other = (Bankable) other;
并不会帮助你,因为变量 other
仍然是类型为 Object
,所以你仍然无法使用它来调用 Bankable
方法。
创建一个 Bankable
变量来代替。
Bankable that = (Bankable) other;
然后你可以调用
int otherBalance = that.getBalance();
英文:
This line
other = (Bankable) other;
doesn't help you because the variable other
is still of type Object
, so you still can't call Bankable
methods using it.
Create a Bankable
variable instead.
Bankable that = (Bankable) other;
then you can call
int otherBalance = that.getBalance();
答案2
得分: 0
将 other = (Bankable) other;
替换为 Bankable bankable = (Bankable) other;
,将 otherBalance = this.getBalance();
替换为 otherBalance = bankable.getBalance();
。
英文:
Replace other = (Bankable) other;
with Bankable bankable = (Bankable) other;
and otherBalance = this.getBalance();
with otherBalance = bankable.getBalance();
.
答案3
得分: 0
看起来你有几个问题:
-
(Bankable) other = other
并没有任何作用,因为other
总是一个Object
类型。尝试创建一个具有getBalance
方法的Bankable
对象:Bankable otherBankable = (Bankable) other;
-
一旦你完成这一步,你可以解决另一个问题,即你将
otherBalance
设置为this.getBalance()
。它应该被设置为otherBankable.getBalance()
。
另一种不涉及创建另一个对象变量的解决方案是在你想获取余额时进行强制类型转换:
```java
otherBalance = ((Bankable) other).getBalance();
```
英文:
Looks like you have a couple issues:
-
other = (Bankable) other
does nothing, sinceother
is always anObject
type. Try creating aBankable
object that hasgetBalance
:Bankable otherBankable = (Bankable) other;
-
Once you have that you can fix the other issue, where you set
otherBalance
tothis.getBalance()
. It should be set tootherBankable.getBalance()
instead.
Another solution that doesn't involve creating another object variable is to just cast when you want to get the balance:
otherBalance = ((Bankable) other).getBalance();
答案4
得分: 0
// compareTo方法的“other”参数应为Bankable,而不是Object:
public int compareTo(Bankable other) { ... }
要使其工作,您的`Bankable`类应实现`Comparable<Bankable>`接口 - 看起来您可能缺少了类型参数`<Bankable>`,否则您可能会得到不同的错误。Bankable类声明应如下所示:
class Bankable implements Comparable<Bankable>;
最后,根据您实现的`compareTo`的方式,它只能返回0或正数。`compareTo`必须能够返回*负数* - 这样它可以告诉您当前“this”对象应该位于“other”对象之前。实现`compareTo`的正确而简洁的方法是:
public int compareTo(Bankable other) {
return Integer.compare(this.getBalance(), other.getBalance());
}
英文:
The "other" parameter for the compareTo method should be Bankable, not Object:
public int compareTo(Bankable other) { ... }
For this to work, your Bankable
class should implement the Comparable<Bankable>
interface - it sounds like you you may be missing the type parameter <Bankable>
or you would be getting a different error. The Bankable class declaration should look like this:
class Bankable implements Comparable<Bankable>
Finally, with the way you have implemented compareTo
, it can only return 0 or a positive number. It must be possible for compareTo
to return negative numbers - that's how it can tell you that the current "this" object should come before the "other" object. A correct and compact way to implement compareTo
is:
public int compareTo(Bankable other) {
return Integer.compare(this.getBalance(), other.getBalance());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论