简体   繁体   English

使用方法改变Java中Object的Static变量的值

[英]Using a Method to Change the Value of a Static Variable of an Object in Java

I am trying to create an ATM in Java using methods.我正在尝试使用方法在 Java 中创建一个 ATM。 I am trying to call methods that change the variable (total) of the object (balance).我正在尝试调用更改 object(余额)的变量(总计)的方法。 I have successfully created the object and set its starting value but I can't figure out how to create a method that alters that methods.我已经成功创建了 object 并设置了它的起始值,但我不知道如何创建一个改变这些方法的方法。 I am not sure how setters and getters would apply because everything is static.我不确定 setter 和 getter 将如何应用,因为一切都是 static。 Any suggestions?有什么建议么?

enter image description here在此处输入图像描述

Your setter and getter could look like in the following snippet.您的 setter 和 getter 可能类似于以下代码段。 I set total to private as it should only be accessible by the setter and getter functions.我将 total 设置为 private,因为它只能由 setter 和 getter 函数访问。 You can then set your value by startVal.setTotal(100);然后您可以通过 startVal.setTotal(100); 设置您的值

class balance {

    private int total;

    int getTotal() {
        return total;
    }

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

} }

You can do like this:你可以这样做:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM