各位朋友看看这个报错是啥原因,研究了半天没研究明白
TestBanking51
package banking51;
public class TestBanking51 {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
// Create several customers and their accounts
bank.addCustomers("Jane", "Simms");
customer = bank.getCustomer(0);
Customer.addAccount(new SavingAccount(500.00, 0.05));
Customer.addAccount(new CheckingAccount(200.00, 400.00));
bank.addCustomers("Owen", "Bryant");
customer = bank.getCustomer(1);
Customer.addAccount(new CheckingAccount(200.00));
bank.addCustomers("Tim", "Soley");
customer = bank.getCustomer(2);
Customer.addAccount(new SavingAccount(1500.00, 0.05));
Customer.addAccount(new CheckingAccount(200.00));
bank.addCustomers("Maria", "Soley");
customer = bank.getCustomer(3);
// Maria and Tim have a shared checking account
Customer.addAccount(bank.getCustomer(2).getAccount(1));
Customer.addAccount(new SavingAccount(150.00, 0.05));
// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");
for ( int cust_idx = 0; cust_idx < bank.getNumberOFCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for ( int acct_idx = 0; acct_idx < customer.getNumberofAccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/* Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
if (account instanceof SavingAccount){
account_type = "Savings Account";
}else if (account instanceof CheckingAccount){
account_type = "Checking Account";
}
// Print the current balance of the account
/* Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
System.out.println(account_type+":current balance is ¥ " + account.getBalance());
}
}
}
}
Customer
package banking51;
//创建kehu类
public class Customer {
// 声明三个私有对象属性:firstName、lastName 和 account。
private String firstName;
private String lastName;
private static Account [] accounts;
private static int numberofAccounts;
// 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
public Customer(String f, String l){
firstName = f;
lastName = l;
accounts = new Account[2];
}
// 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName
// 返
// 回相应的属性。
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
// 声明 setAccount 方法来对 account 属性赋值。
public static void addAccount(Account account) {
accounts[numberofAccounts++] = account;
}
// 声明 getAccount 方法以获取 account 属性。
public Account getAccount(int index) {
return accounts[index];
}
public int getNumberofAccounts(){
return numberofAccounts;
}
}
31行这里数组越界了吧 ,实例化对象的时候创建的是2个长度的,超过两个就报错了吧
我理解你程序的大概意思是一个客户可以有两个银行账户是吗?如果是的话,有这么几个问题。第一,你的customer类中定义了account数组是static属性,属于类,不是属于该类的每个对象,也就是说该类的所有对象公有这个属性,你调用AddAccount方法的时候,是使用类名调用类方法的,每次调用你的numberofaccounts都会自增一,而你的这个accounts数组长度为2,所以当你调用三次及以上的时候,数组下标就会超过1,造成数组越界,也就是你截的图,arrayindexoutofbounds就是数组越界异常的意思。如果你想实现每个客户可以有两个银行账户的话,可以将accounts数组改为普通成员变量,addaccount方法改为普通成员方法,希望对你有所帮助,有帮助的话采纳一下吧,也可以私信讨论哦