java--用一個對象實現集合

來源:互聯網
上載者:User

標籤:orm   封裝   common   tor   主函數   print   text   mat   pre   

首先先建立一個類:

package collection;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;public class Employ {//javabean 實體類  java豆 只包含屬性和     private int id;     private String name;     private int salary;     private String department;     private Date hiredata;          DateFormat format=new SimpleDateFormat("yyyy-yy");                             public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }        public Employ(int id, String name, int salary, String department,            String hiredata) {        super();        this.id = id;        this.name = name;        this.salary = salary;        this.department = department;        //this.hiredata = hiredata;        DateFormat format=new SimpleDateFormat("yyyy-yy");        try {            this.hiredata=format.parse(hiredata);        } catch (Exception e) {            // TODO: handle exception        }     }    public Employ() {        // TODO Auto-generated constructor stub    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getSalary() {        return salary;    }    public void setSalary(int salary) {        this.salary = salary;    }    public String getDepartment() {        return department;    }    public void setDepartment(String department) {        this.department = department;    }    public Date getHiredata() {        return hiredata;    }    public void setHiredata(Date hiredata) {        this.hiredata = hiredata;    }     }

測試主函數:

package collection;

import java.util.ArrayList;

public class Datacuncu {
public static void main(String[] args) {
//一行記錄一個對象來封裝
Employ e=new Employ(0301,"kl",3000,"yc","2007-10");
Employ e1=new Employ(0301,"kla",3200,"ycs","2007-10");
Employ e2=new Employ(0301,"kls",3500,"yca","2007-10");
ArrayList<Employ>list=new ArrayList<Employ>();
list.add(e);
list.add(e1);
list.add(e2);
printEmploy(list);
}
public static void printEmploy(ArrayList<Employ> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
System.out.println(list.get(i).getHiredata());
System.out.println(list.get(i).getDepartment());
}
}
}

運行結果:

 



 

java--用一個對象實現集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.