import java.text.SimpleDateFormat; import java.util.Date; /** * 定义私有属性: * String name; * int age; * String gender; * int salary; * Date hiredate;//入职时间 * * 定义构造方法,以及属性get,set方法. * 定义toString方法,格式如: * 姓名:张三,年龄:25,性别:男,薪资:5000,入职时间:2006-02-15 * * 定义equals方法,要求名字以及年龄相同,则认为内容一致。 * * 实现Comparable接口,并重写抽象方法comparaTo(),比较规则为年龄小的人小。 * @author Xiloer * */ public class Emp implements Comparable{ private String name; private int age; private String gender; private int salary; private Date hiredate; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public Emp(String name, int age, String gender, int salary, Date hiredate) { // TODO Auto-generated method stub this.name = name; this.age = age; this.gender = gender; this.salary = salary; this.hiredate = hiredate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } @Override public boolean equals(Object obj) { if(obj instanceof Emp) { Emp emp = (Emp) obj; Emp emp1 = (Emp) obj; return name.equals(emp.name) && age==emp1.age; } return false; } @Override public String toString() { return "姓名:"+name+",年龄:"+age+",性别:"+gender+",薪资:"+salary+",入职时间:"+sdf.format(hiredate); } /*对于返回值,如果调用compareTo()方法的对象等于参数,则返回0, *如果大于参数,则返回一个大于0的值, *如果小于参数,则返回一个小于0的值 */ @Override public int compareTo(Object o) { Emp e = (Emp) o; if(age>e.age) return 1; if(age<e.age) return -1; return 0; } }