• java集合练习


    import java.util.ArrayList;
    import java.util.List;
    
    public class Bank{
        private List<Customer> customers;             //customers集合
        private int numberOfCustomers;                //记录客户数量
        public Bank(){                                //构造方法,初始化customers
            customers=new ArrayList<Customer>();
        }
        public void addCustomer(String f,String l){        //方法:往customers里面添加Customer对象
            Customer p=new Customer(f,l);
            customers.add(p);
        }
        public int getNumOfCustomers(){                   //获取客户数量,即获取customers的长度
            numberOfCustomers=customers.size();
            return numberOfCustomers;
        }
        public Customer getCustomer(int index){            //根据索引取值return customers.get(index); 
      }
    }
    复制代码
    复制代码
    public class TestBanking {
        public static void main(String[] args) {
            Bank cus=new Bank(){};
            cus.addCustomer("Simms", "Jane");
            cus.addCustomer("Bryant", "Owen");
            cus.addCustomer("Soley","Tim");
            cus.addCustomer("Soley","Maria");
            for(int i=0;i<cus.getNumOfCustomers();i++){
                System.out.println("Customer "+(i+1)+" is"+" "+cus.getCustomer(i));
            }    
            System.out.println("当前客户数量="+cus.getNumOfCustomers());
            System.out.println("第二个客户是:"+cus.getCustomer(2));
        }
    }

    public class Customer {
        //声明私有属性
        private String firstName;
        private String lastName;
        private Account account;
        //声明构造器
        Customer(){};
        Customer(String f,String l){
            this.firstName=f;
            this.lastName=l;
        }
        //声明存取器getter/setter方法
        public String getFirstName() {                    //firstName
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {                    //lastName
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        //存取器getter/setter方法——account
        public Account getAccount() {
            return account;
        }
        public void setAccount(Account acct) {
            this.account=acct;
        }
        @Override
        public String toString() {
            return firstName + "," + lastName;
        }
    }
  • 相关阅读:
    PHP单引号和双引号的区别
    thinkphp实现导航高亮的简单方法
    js选中当前菜单后高亮显示的导航条
    Dubbo新版管控台
    把本人基于Dubbo的毕业设计分享粗来~
    Windows 查看端口占用情况
    GitLab 环境搭建【CentOS7】
    【非专业前端】vue+element+webpack
    【非专业前端】使用vue2.5.17+element2.4.5
    ElasticSearch6.3.2------入门
  • 原文地址:https://www.cnblogs.com/zhaocundang/p/7768842.html
Copyright © 2020-2023  润新知