比较对象并赋予正确的值。

huangapple go评论82阅读模式
英文:

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 &gt; getBalance()) {
     return otherBalance - getBalance();
  }
  else if (otherBalance &lt; 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, since other is always an Object type. Try creating a Bankable object that has getBalance:

    Bankable otherBankable = (Bankable) other;

  • Once you have that you can fix the other issue, where you set otherBalance to this.getBalance(). It should be set to otherBankable.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&lt;Bankable&gt; interface - it sounds like you you may be missing the type parameter &lt;Bankable&gt; or you would be getting a different error. The Bankable class declaration should look like this:

class Bankable implements Comparable&lt;Bankable&gt;

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

huangapple
  • 本文由 发表于 2020年8月20日 04:19:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63494422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定