以下是我作为课程练习所做的 Java 代码。我有一个名为 SavingsAccount 的类。它有余额和利息变量。但是,我将它们设置为公开,但如果我想单独处理帐户,是否需要将它们设为私有并为这些变量提供“get/set”方法?我可以不公开变量吗?其余代码具有使用这些变量进行计算的方法。
public class SavingsAccount { //This class has three different variables that define it
public double balance; //Double for account balance
public static double annualInterestRate; //Class method for interest rate
public final int ACCOUNT_NUMBER; //Constant int for keeping track of accounts
public SavingsAccount (int ACCOUNT_NUMBER, double balance) { //Constructor that takes down account number and balance to keep track of
this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
this.balance = balance;
}