私は、Java での継承を調べる宿題に取り組んでいます。サブクラスからスーパークラスの配列にアクセスする方法を理解するのに少し苦労しています。私は他のいくつかの質問を見ましたが、私はJavaに慣れていないので、まだよく理解していません.
スーパークラスはこちら
import java.text.NumberFormat;
/**
* Bank represents a single Bank containing a number of BankAccounts.
*/
public class Bank {
// Member variables:
/** The array of BankAccount objects contained in this bank. */
protected BankAccount[] myAccounts = new BankAccount[2000];
/** The number of BankAccount objects stored in the array in this bank. */
protected int numberOfAccounts = 0;
// Constructors:
/**
* Creates a Bank.
*/
public Bank() {}
// Methods:
/**
* Creates an account with the name and balance, and adds it to
* the bank's list of accounts.
* If the name already exists, no account will be created.
* @param aName The name for the new account.
* @param aBalance The initial balance for the new account.
*/
public void createAccount( String aName, double aBalance) {
BankAccount existing = this.findAccount( aName);
if( existing != null) return;
BankAccount anAccount = new BankAccount( aBalance, aName);
this.myAccounts[ numberOfAccounts] = anAccount;
this.numberOfAccounts++;
}
/**
* Finds an account in the bank's list of accounts by name.
* If no account is found, this method returns null.
* @param aName The name of the BankAccount to search for.
* @return The BankAccount bearing the given name, if found.
*/
public BankAccount findAccount( String aName) {
BankAccount answer = null;
for( int index = 0; index < numberOfAccounts; index++) {
BankAccount anAccount = this.myAccounts[ index];
if( aName.equals( anAccount.getName())) {
return( anAccount);
}
}
return( answer);
}
/**
* Returns a String which represents a short summary of
* all the accounts in the bank.
* @return A String representation of all accounts and their balances in the bank.
*/
public String toString() {
String answer = "";
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
for( int index = 0; index < numberOfAccounts; index++) {
BankAccount anAccount = this.myAccounts[ index];
String money = currencyFormatter.format( anAccount.getBalance());
answer += anAccount.getName() + " \t" + money + "\n";
}
return( answer);
}
}
これがサブクラスの始まりです
public class BankSubClass extends Bank{
private double interestPaid;
// Constructor
public BankSubClass(String aName, double aBalance, double aInterest) {
super();
this.interestPaid = aInterest;
}
// Getters
public double getInterestPaid() {return(this.interestPaid);}
// Setters
public void setInterestPaid(double setInterestPaid) {this.interestPaid = setInterestPaid;}
// Other methods
public double endOfYear() {
for (i=0;i<BankAccount.length();i++) {
}
}
}
最後の for ループは、物事が少しつまずいているところです。Netbeans は、「シンボルが見つかりません: 変数 i」というエラーをスローしています。たぶん、これは私が使用しようとしている銀行口座配列とは何の関係もありません。わかりません。どんな助けでも大歓迎です
御時間ありがとうございます!
編集
では、同じ宿題の続きです
皆さんの返信に感謝します。あなたの提案でその問題は解決しましたが、現在、別のスピード バンプにぶつかっています。for ループが含まれているこのメソッドの背後にあるアイデアは、BankAccount オブジェクトの配列を実行し、それらのいずれかが InterestAccount タイプ (以前に作成したクラス) であるかどうかを確認し、そうである場合は yearlyUpdate() を呼び出すことです。そのクラスのメソッド
これがInterestAccountクラスです
public class InterestAccount extends BankAccount {
private double interestRate;
// Constructor
/**
* Create and interest bearing bank account with a balance, name,
* and interest rate
* @param aBalance The balance of the account
* @param aName The name tied to the account
* @param myInterestRate The interest rate of the account
*/
public InterestAccount(double aBalance, String aName, double myInterestRate) {
super(aBalance, aName);
this.interestRate = myInterestRate;
}
// Getters
/**
* Gets the interest rate of the account
* @return the interest rate of the account
*/
public double getInterestRate() {return(this.interestRate);}
// Setters
/**
* Sets the interest rate of the account
* @param interestSet The new interest rate of the account
*/
public void setInterestRate(int interestSet) {this.interestRate = interestSet;}
// Other Methods
/**
* Calculates the interest earned on the account over a year
* @return the interest earned over a year
*/
public double yearlyUpdate() {
double answer = (super.getBalance()*this.interestRate);
return answer;
}
}
これが私が現在取り組んでいるスーパークラスです
import java.text.NumberFormat;
/**
* Bank represents a single Bank containing a number of BankAccounts.
*/
public class Bank {
// Member variables:
/** The array of BankAccount objects contained in this bank. */
protected BankAccount[] myAccounts = new BankAccount[2000];
/** The number of BankAccount objects stored in the array in this bank. */
protected int numberOfAccounts = 0;
// Constructors:
/**
* Creates a Bank.
*/
public Bank() {}
// Methods:
/**
* Creates an account with the name and balance, and adds it to
* the bank's list of accounts.
* If the name already exists, no account will be created.
* @param aName The name for the new account.
* @param aBalance The initial balance for the new account.
*/
public void createAccount( String aName, double aBalance) {
BankAccount existing = this.findAccount( aName);
if( existing != null) return;
BankAccount anAccount = new BankAccount( aBalance, aName);
this.myAccounts[ numberOfAccounts] = anAccount;
this.numberOfAccounts++;
}
/**
* Finds an account in the bank's list of accounts by name.
* If no account is found, this method returns null.
* @param aName The name of the BankAccount to search for.
* @return The BankAccount bearing the given name, if found.
*/
public BankAccount findAccount( String aName) {
BankAccount answer = null;
for( int index = 0; index < numberOfAccounts; index++) {
BankAccount anAccount = this.myAccounts[ index];
if( aName.equals( anAccount.getName())) {
return( anAccount);
}
}
return( answer);
}
/**
* Returns a String which represents a short summary of
* all the accounts in the bank.
* @return A String representation of all accounts and their balances in the bank.
*/
public String toString() {
String answer = "";
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
for( int index = 0; index < numberOfAccounts; index++) {
BankAccount anAccount = this.myAccounts[ index];
String money = currencyFormatter.format( anAccount.getBalance());
answer += anAccount.getName() + " \t" + money + "\n";
}
return( answer);
}
}
最後に、この for ループを実行しようとしているサブクラスを次に示します。
public class BankSubClass extends Bank{
private double interestPaid;
// Constructor
public BankSubClass(String aName, double aBalance, double aInterest) {
super();
this.interestPaid = aInterest;
}
// Getters
public double getInterestPaid() {return(this.interestPaid);}
// Setters
public void setInterestPaid(double setInterestPaid) {this.interestPaid = setInterestPaid;}
// Other methods
public double endOfYear() {
double trackInterest=0;
for (int i=0;i<numberOfAccounts;i++) {
BankAccount working = myAccounts[i];
boolean hasInterest = working instanceof InterestAccount;
if (hasInterest) {
trackInterest = trackInterest + working.yearlyUpdate();
}
return trackInterest;
}
}
}
現在、netbeans は「作業中」に呼び出そうとしたときに「yearlyUpdate()」メソッドを見つけることができません。以前のコードで作業中のオブジェクトが InterestAccount タイプであることが確認されたため、そのメソッドを使用できる必要があります。
助けてくれてありがとう!