UML设计:
文件组织:
(注: 在bank4中,直接调用bank3的内容, 不再重复编写代码即可!)
代码编写Bank.java:
package Banking_4; import Banking_3.*; public class Bank { private Customer[] customers ; //用于存放客户 private int numberofCustomers; //用于记录Customer的个数 public Bank(){ numberofCustomers=0; customers = new Customer[100]; ///这里记得要初始化!不然要发生java.lang.NullPointerException } public void addCustomer(String f,String l){ int i=this.numberofCustomers++; customers[i]=new Customer(f,l);//新建一个构造对象 } public int getNumOfCustomers() { return numberofCustomers; } public Customer getCustomer(int index) { return customers[index]; } }
2/TestBanking4.java
package Banking_4; import Banking_3.*; public class Bank { private Customer[] customers ; //用于存放客户 private int numberofCustomers; //用于记录Customer的个数 public Bank(){ numberofCustomers=0; customers = new Customer[100]; ///这里记得要初始化!不然要发生java.lang.NullPointerException } public void addCustomer(String f,String l){ int i=this.numberofCustomers++; customers[i]=new Customer(f,l);//新建一个构造对象 } public int getNumOfCustomers() { return numberofCustomers; } public Customer getCustomer(int index) { return customers[index]; } }
运行结果:
Customer [1] is Simms, Jane Customer [2] is Bryant, Owen Customer [3] is Soley, Tim Customer [4] is Soley, Maria