标签:
用数组表示多重性
练习目标-在类中使用数组作为模拟集合操作: 在本练习中,将用数组实现银行与客户间的多重关系。
任务
对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。
Customer [1] is Simms,Jane
Customer [2] is Bryant,Owen
Customer [3] is Soley,Tim
Customer [4] is Soley,Maria
package banking; import java.lang.reflect.Array; import java.util.Iterator; import java.util.List; public class Bank { //成员属性 private String customers[ ] ; private int numberOfCustomers = 1 ; //构造方法 public Bank( ) { } public Bank( String customers[ ] ) { this.customers = new String[6] ; this.customers = customers ; } public String[] addCustomer(String firstName ,String lastName) { System.out.println("customers["+numberOfCustomers+"]"+" is "+firstName+" , "+lastName); numberOfCustomers++ ; return customers ; } public int getNumberOfCustomers() { return numberOfCustomers; } }
Bank bk =new Bank( ) ; bk.addCustomer( "Simms" , "Jane" ); bk.addCustomer( "Bryant" , "Owen" ); bk.addCustomer( "Soley" , "Tim" ); bk.addCustomer( "Soley" , "Maria" );
标签:
原文地址:http://www.cnblogs.com/20gg-com/p/5905943.html