[英]Passing Variables to Extended Class and Calling Methods
I am at a complete loss right now. 我现在完全不知所措。 I am trying to develop a program that will display the
Months
, Account #
, and Balance
of two savings accounts and update the Balance
as interest on the accounts is accrued: 我正在尝试开发一个程序,该程序将显示两个储蓄帐户的
Months
, Account #
和Balance
,并在应计利息时更新Balance
:
I have to design four individual classes in order to do this. 为此,我必须设计四个单独的类。
SavingsAccount
, SavingsAccountDriver
, FlexibleSavingsAccount
, and CDSavingsAccount
. SavingsAccount
, SavingsAccountDriver
, FlexibleSavingsAccount
和CDSavingsAccount
。 SavingsAccount
is the parent class of both FlexibleSavingsAccount
and CDSavingsAccount
. SavingsAccount
是FlexibleSavingsAccount
和CDSavingsAccount
的父类。 SavingsAccountDriver
is the Main class. SavingsAccountDriver
是Main类。
In SavingsAccount
I have a method setAnnualInterestRate()
that is called in SavingsAccountDriver
. 在
SavingsAccount
我有一个方法setAnnualInterestRate()
在SavingsAccountDriver
调用。 This method sets the interest rate for each account. 此方法设置每个帐户的利率。 The problem I am having is passing this value to the extended classes
FlexibleSavingsAccount
and CDSavingsAccount
so that I can update the balance by adding the interest rate for each account. 我遇到的问题是将此值传递给扩展类
FlexibleSavingsAccount
和CDSavingsAccount
以便我可以通过添加每个帐户的利率来更新余额。 If anyone could please assist me on how this is done I would greatly appreciate it. 如果有人可以帮助我完成这项工作,我将不胜感激。
SavingsAccountDriver : 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;
}
}*/
}
}
SavingsAccount : 储蓄账户 :
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;
}
}
FlexibleSavingsAccount : FlexibleSavingsAccount :
public class FlexibleSavingsAccount extends SavingsAccount{
public FlexibleSavingsAccount(int account_number, double balance) {
super(account_number, balance);
}
//returns interest
public double getInterest() {
return annualInterestRate;
}
}
You may want to add getInterest as an abstract method and declare SavingsAccount as abstract class. 您可能需要将getInterest添加为抽象方法,并将SavingsAccount声明为抽象类。 then you will have the method known by the compiler to call and you will be forced in the subclasses to provide the proper implementation
那么您将拥有编译器已知的方法来调用,并且您将被迫在子类中提供正确的实现
Your getInterest()
method should be declared in your parent class - SavingsAccount
- if you want to declare saver1
and saver2
as being of type SavingsAccount
. 如果要将
saver1
和saver2
声明为SavingsAccount
类型, saver2
在父类SavingsAccount
声明您的getInterest()
方法。
The way that you show, the method will only be available to classes declared as FlexibleSavingsAccount
. 按照您显示的方式,该方法仅适用于声明为
FlexibleSavingsAccount
类。
Since you declared them as SavingsAccount
you only have access to that class' methods. 由于您将它们声明为
SavingsAccount
您只能访问该类的方法。
Child classes can access the methods of the parent but not the other way around. 子类可以访问父类的方法,但不能相反。
You have declared your instantiated savings accounts as type SavingsAccount
. 您已将实例化的储蓄帐户声明为
SavingsAccount
类型。 They will not have access to the method getInterest()
since it is in the child class FlexibleSavingsAccount
. 他们将无法访问方法
getInterest()
因为该方法位于子类FlexibleSavingsAccount
。
You need to instantiate them as the actual savings account that you wish them to actually be: 您需要将它们实例化为希望它们实际成为的实际储蓄帐户:
FlexibleSavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object
Now saver1
will be able to access getInterest()
. 现在
saver1
将能够访问getInterest()
。
Extra idea: 额外的主意:
What might be nicer is to code the parent SavingsAccount
as an interface. 更好的方法是将父
SavingsAccount
编码为接口。 You would then declare your getInterest()
method in this interface, whilst leaving the details of what goes in the method to your children classes. 然后,您将在此接口中声明
getInterest()
方法,同时将方法中的详细信息留给子类。
SavingsAccount: 储蓄账户:
public interface SavingsAccount {
public int getAccountNumber();
public double getBalance();
public void setAnnualInterestRate (double interestRate);
}
Then instantiate your accounts: 然后实例化您的帐户:
SavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object
Note this has the added benefit that you declare your instances coded to the SavingsAccount
interface which is always a nice idea for future proofing your code. 请注意,这样做还有一个好处,就是您可以将实例声明为已编码到
SavingsAccount
接口,这对于将来验证代码始终是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.