java - 在 Java 中更改字段值时遇到问题

标签 java

所以,作为免责声明,我对编程非常陌生,可能遗漏了一些明显的东西。现在,我正在尝试创建两个名为 withdraw() 和 deposit() 的方法,它们允许我更改字段 currentBalance 的值,但是,每次我尝试使用我的两个方法更改 currentBalance 的值时,我都会检查使用我的方法 getBalance() 获取该字段的值,它始终保持不变。

class TradingService{

   public class Trader {

      //This field stores the trader's name
      private String traderName;

      //This field stores the trader's current balance
      private double currentBalance;

      //A constructor to create a Trader
      public Trader(String traderName) {
        this.traderName = traderName;
      }

      //This method gets returns the trader's name
      public String getName() {
        return traderName;
      }

      //This method set's the trader's name
      public void setName(String traderName) {
        this.traderName = traderName;
      }

      //This method decreases the trader's balance
      public void withdraw(double withdrawAmount) {
        this.currentBalance = (currentBalance - withdrawAmount);
      }

      //This method increases the trader's balance
      public void deposit(double depositAmount) {
        this.currentBalance = (currentBalance + depositAmount);
      }

      //This method returns the trader's current balance
      public double getBalance() {
        return currentBalance;
      }

   }

}

我正在使用 DrJava 并在交互面板中测试我的代码。这是我的测试结果。

> Trader t1
> t1 = new Trader("Bill")
Trader@22e1cbe4
> t1.deposit(10.0)
10.0
> t1.getBalance()
0.0

我已尽我所能来修复代码,但我没有想法,而且我认为在我的代码中再输入 3 个小时的随机内容不会有多大作用。

感谢您花时间阅读我的问题。

最佳答案

“交易员”类看起来不错,适合我。但是你有一个“Trader”什么是“TradingService”类中的公共(public)类,我想也许这个类没有编译并且你正在执行一个旧文件,你可以像内部类一样用“Trader”类试试这个。

class Trader {

  //This field stores the trader's name
  private String traderName;

  //This field stores the trader's current balance
  private double currentBalance;

  //A constructor to create a Trader
  public Trader(String traderName) {
    this.traderName = traderName;
  }

  //This method gets returns the trader's name
  public String getName() {
    return traderName;
  }

  //This method set's the trader's name
  public void setName(String traderName) {
    this.traderName = traderName;
  }

  //This method decreases the trader's balance
  public void withdraw(double withdrawAmount) {
    this.currentBalance = (currentBalance - withdrawAmount);
  }

  //This method increases the trader's balance
  public void deposit(double depositAmount) {
    this.currentBalance = (currentBalance + depositAmount);
  }

  //This method returns the trader's current balance
  public double getBalance() {
    return currentBalance;
  }
}

public class TradingService{

    public static void main(String[] args)
    {

      Trader t = new Trader("test");
      t.deposit(10);
      System.out.print(t.getBalance());

    }
}

关于java - 在 Java 中更改字段值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904562/

相关文章:

java - jsoup : parse data of p tag which is between every h2 tag

Java AffinetransformOp filter() 搞砸了我的形象

java - 使用 hibernate 工具进行惰性映射?

Java Applets-在不使用 'canvas' 的情况下创建交互式交通灯?

java - 提高java套接字的通信速度

java - jcabi ssh 将输出重定向到 null 或文件

java - 图像不动

Java toTitleCase 函数

java - 按出现次数对子字符串进行降序排序 - Java

java - 在 Android 中获取键盘语言或检测用户输入语言