//用数组实现银行与客户的多重关系
package banking4;
public class Bank {
private Customer[] customers; private int numberOfCustomer;//用于记录customer个数 public Bank(){ customers=new Customer[5]; } public void addCustomer(String f,String l){ Customer cust=new Customer(f,l); customers[numberOfCustomer]=cust; numberOfCustomer++; } public int getNumOfCustomers(){//获取customer个数 return numberOfCustomer; } public Customer getCustomer(int index){//返回指定索引位置 return customers[index]; }}
package testbanking1;
import banking4.*;public class testbanking4 { public static void main(String[] args){ Bank bank=new Bank(); bank.addCustomer("吕","青青"); bank.addCustomer("吕", "荣玉"); bank.addCustomer("吕", "跃文"); bank.addCustomer("顾", "怀英"); for(int i=0;i<bank.getNumOfCustomers();i++){ Customer customer=bank.getCustomer(i); System.out.println("customer:"+i+customer.getFirstName()+customer.getLastName()); }}}