gpt4 book ai didi

java - 将变量传递给扩展类并调用方法

转载 作者:行者123 更新时间:2023-11-29 09:32:15 25 4
gpt4 key购买 nike

我现在完全不知所措。我正在尝试开发一个程序来显示两个储蓄账户的 MonthsAccount #Balance 并更新 Balance 因为帐户的利息是应计的:

  • 第一个账户每月产生 10,002 利息,年利率为 1.2%。
  • 对于第二个帐户,10,003 利息按季度计息,年利率为 4%。

为了做到这一点,我必须设计四个单独的类。 SavingsAccountSavingsAccountDriverFlexibleSavingsAccountCDSavingsAccountSavingsAccountFlexibleSavingsAccountCDSavingsAccount 的父类。 SavingsAccountDriver 是主类。

SavingsAccount 中,我有一个在 SavingsAccountDriver 中调用的方法 setAnnualInterestRate()。此方法设置每个帐户的利率。我遇到的问题是将此值传递给扩展类 FlexibleSavingsAccountCDSavingsAccount 以便我可以通过添加每个帐户的利率来更新余额。如果有人可以帮助我完成此操作,我将不胜感激。

SavingsAccountDriver:

public class SavingsAccountDriver {
public static void main (String[] args) {
SavingsAccount saver1 = new SavingsAccount(10002, 2000); //create new SavingsAccount object
SavingsAccount saver2 = new SavingsAccount(10003, 3000); //create new SavingsAccount object

saver1.setAnnualInterestRate(.012); //sets AnnualInterestRate for 'saver1' object
saver2.setAnnualInterestRate(.04); //sets AnnualInterestRate for 'saver2' object

System.out.println("\nMonthly balances:\n");
System.out.println("Month " + " Account# " + " Balance " + " " + " Month " + " Account# " + " Balance ");
System.out.println("----- " + " -------- " + " ------- " + " " + " ----- " + " -------- " + " ------- ");


System.out.println(saver1.getAccountNumber() + " / " + saver1.getBalance() + " / " + saver1.getInterest());
System.out.println(saver2.getAccountNumber() + " / " + saver2.getBalance() + " / " + saver2.getInterest());

/*for(int month = 0; month <= 12; month++) {
switch(month) { // switch that outputs month, account number, and balance for both accounts (Some non-needed cases used to make output look cleaner)
case 0:
System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
case 4:
saver1.addInterest();
//saver2.addInterest();

System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
case 10:
saver1.addInterest();
//saver2.addInterest();

System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
case 11:
saver1.addInterest();
//saver2.addInterest();

System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
case 12:
saver1.addInterest();
//saver2.addInterest();

double totalBalance = saver1.getBalance() + saver2.getBalance();
System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
default:
saver1.addInterest();
//saver2.addInterest();

System.out.println(month + " " + saver1.getAccountNumber() + " " + saver1.getBalance() + " " + month + " " + saver2.getAccountNumber() + " " + saver2.getBalance());
break;
}
}*/
}
}

储蓄账户:

public class SavingsAccount {
// variables specific to SavingsAccount class
public double annualInterestRate;
private final int ACCOUNT_NUMBER;
public double balance;

//constructor with account number and balance parameters
public SavingsAccount(int account_number, double balance) {
this.ACCOUNT_NUMBER = account_number;
this.balance = balance;
}

//returns account number
public int getAccountNumber() {
return this.ACCOUNT_NUMBER;
}

//returns balance
public double getBalance() {
return this.balance;
}

//sets interest rate
public void setAnnualInterestRate (double interestRate) {
this.annualInterestRate = interestRate;
}
}

灵活储蓄账户:

public class FlexibleSavingsAccount extends SavingsAccount{

public FlexibleSavingsAccount(int account_number, double balance) {
super(account_number, balance);
}

//returns interest
public double getInterest() {
return annualInterestRate;
}
}

最佳答案

您可能希望将 getInterest 添加为抽象方法并将 SavingsAccount 声明为抽象类。然后你将拥有编译器已知的方法来调用,你将被迫在子类中提供正确的实现

关于java - 将变量传递给扩展类并调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25652091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com