gpt4 book ai didi

Java从子类访问 protected 变量

转载 作者:行者123 更新时间:2023-11-30 11:46:02 26 4
gpt4 key购买 nike

我正在做一个关于 java 继承的家庭作业。我在理解如何从子类访问父类(super class)中的数组时遇到了一些麻烦。我查看了其他几个问题,但由于我对 Java 还很陌生,所以我还是不太明白。

这是父类(super class)

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;
}
}

这是我目前正在使用的父类(super class)

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 类型,它应该有该方法可用

感谢您的帮助!

最佳答案

// Other methods
public double endOfYear() {
for (i=0;i<BankAccount.length();i++) {

}
}

你需要声明i

// Other methods
public double endOfYear() {
for (int i=0;i<BankAccount.length();i++) {

}
}

关于Java从子类访问 protected 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022548/

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