【问题标题】:Using a Method to Change the Value of a Static Variable of an Object in Java在 Java 中使用方法更改对象的静态变量的值
【发布时间】:2020-07-18 16:22:16
【问题描述】:

我正在尝试使用方法在 Java 中创建 ATM。我正在尝试调用更改对象(余额)的变量(总计)的方法。我已经成功地创建了对象并设置了它的起始值,但我不知道如何创建一个改变这些方法的方法。我不确定 setter 和 getter 将如何应用,因为一切都是静态的。有什么建议吗?

enter image description here

【问题讨论】:

标签: java methods static setter


【解决方案1】:

您的 setter 和 getter 可能如下面的 sn-p 所示。我将 total 设置为 private,因为它只能由 setter 和 getter 函数访问。然后您可以通过 startVal.setTotal(100); 设置您的值;

class balance {

    private int total;

    int getTotal() {
        return total;
    }

    void setTotal(int total) {
        this.total = total;
    } 

}

【讨论】:

    【解决方案2】:

    你可以这样做:

    import java.util.Scanner;
    class Main {
    
    private static Scanner in;
    private static Balance balance;
    
    public static void main (String[] args) {
      in = new Scanner(System.in);
      balance = new Balance();
    
      while(true) {
        System.out.println("Main menu:");
        System.out.println("1. Check Balance");
        System.out.println("2. Withdraw");
        System.out.println("3. Deposite");
        System.out.println("4. Exit");
    
        int n = in.nextInt();
    
        switch (n) {
          case 1 : displayBalance();
                   break;
          case 2 : withdraw();
                   break;
          case 3 : deposite();
                   break;
          default : return;
        }
      }
    }
    
    private static void displayBalance() {
      System.out.println(balance.getBalance());
    }
    
    private static void withdraw() {
      double currentBalance = balance.getBalance();
    
      System.out.println("Enter amount to be withdrawn : ");
      double withdrawAmount = in.nextDouble();
      double balanceAfterWithdraw = currentBalance - withdrawAmount;
    
      if (balanceAfterWithdraw > 0) {
        balance.setBalance(balanceAfterWithdraw);
        System.out.println(withdrawAmount + " withdrawn.");
        System.out.println("Your new balance is : " + balance.getBalance());
      } else
        System.out.println("You don't have sufficient balance !!!");
    }
    
    private static void deposite() {
      double currentBalance = balance.getBalance();
    
      System.out.println("Enter amount to deposit : ");
      double depositeAmount = in.nextDouble();
      double balanceAfterDeposit = currentBalance + depositeAmount;
    
      balance.setBalance(balanceAfterDeposit);
      System.out.println(depositeAmount + " deposited.");
      System.out.println("Your new balance is : " + balance.getBalance());
      }
    }
    
    
    public class Balance {
    
      private double balance;
    
      public Balance() {
        this.balance = 100;
      }
    
      public double getBalance() {
        return balance;
      }
    
      public void setBalance(double amount) {
        this.balance = amount;
      }
    
    }
    

    【讨论】:

    • 非常感谢您的帮助。如果你有时间解释一下,我可以问几个问题吗?我试图为程序提供 100 的初始余额,但我不知道在哪里插入:'balance.setBalance(100);'在代码中以设置值。
    • 创建一个构造函数,它将在对象创建时设置 100,例如 public Balance() { this.balance = 100; } 我正在使用构造函数编辑代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多