我创建了一个程序并将客户对象分配给客户数组,但是当我尝试获取数组中的对象时,它返回 null。我是Java新手,请帮我找出哪里出错了。
public class Customer {
private String firstname,lastname;
public Customer(String f,String l){
this.firstname = f;
this.lastname = l;
}
public String getFirstName(){
return firstname;
}
public String getLastName(){
return lastname;
}
}
public class Bank {
private Customer [] customers;
private int numberofCustomers;
public Bank(){
customers = new Customer [5];
numberofCustomers = 0;
}
public void addCustomer(String f,String l){
int i = numberofCustomers++;
customers[i] = new Customer(f,l);
}
public int getNumberofCustomer(){
return numberofCustomers;
}
public Customer getCustomerMethod(int index){
return customers[index];
}
}
public class TestAccount {
public static void main (String [] args){
Bank b = new Bank();
b.addCustomer("Test", "LastName");
System.out.print(b.getNumberofCustomer());
System.out.print(b.getCustomerMethod(1));
}
}