我有一个名为 savingAccount 的对象,当我在 main 上创建两个这种类型的对象时,我可以设置用户初始帐户余额。当我创建第一个对象时,初始值设置为 3000,当我创建第二个对象时,我的第一个和第二个对象的初始值都设置为 2000
首先,我认为这与公共方法和类有关,但我将它们全部设置为私有、受保护等,问题仍然存在。
这是我的对象:
public class SavingsAccount {
private static double annualInterestRate = 0;
private static double savingsBalance = 0;
private static double monthlyInterest = 0;
SavingsAccount(double savingsBalance){
this.savingsBalance = savingsBalance;
}
void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
private static void updateSavingsBalance(){
savingsBalance = savingsBalance + monthlyInterest;
}
double getSavingsBalance(){
return savingsBalance;
}
}
这是我的司机:
SavingsAccount saver1 = new SavingsAccount(3000.00);
System.out.println("User 1 balance: " + saver1.getSavingsBalance()); // 3000
SavingsAccount saver2 = new SavingsAccount(2000.00);
System.out.println("User 2 balance: " + saver2.getSavingsBalance()); // 2000
//2000 for both now
System.out.println("User 1 balance: " + saver1.getSavingsBalance());
System.out.println("User 2 balance: " + saver2.getSavingsBalance());
回首忆惘然
相关分类