用Java定义一个 Employee 类,该类包含:private 成员变量 name,age,birthday,其中 birthday 为 MyDate 类的对象; 并为每一个属性定义 getter, setter 方法; 并重写 toString 方法输出 name, age, birthday MyDate 类包含: private 成员变量 month,day,year;并为每一个属性定义 getter, setter 方法; 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(TreeSet 需使用泛型来定义) 分别按以下两种方式对集合中的元素进行排序,并遍历输出: 1). 使 Employee 继承 Comparable 接口,并按 name 排序 2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
时间: 2024-03-26 20:41:02 浏览: 100
设计一个名为MyDate.zip
以下是 Java 代码实现:
```java
import java.util.*;
class MyDate {
private int month;
private int day;
private int year;
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
class Employee implements Comparable<Employee> {
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
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 MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public int compareTo(Employee other) {
return this.name.compareTo(other.getName());
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday.getMonth() + "/" + birthday.getDay() + "/" + birthday.getYear() +
'}';
}
}
class BirthdayComparator implements Comparator<Employee> {
@Override
public int compare(Employee e1, Employee e2) {
int yearDiff = e1.getBirthday().getYear() - e2.getBirthday().getYear();
if (yearDiff != 0) {
return yearDiff;
}
int monthDiff = e1.getBirthday().getMonth() - e2.getBirthday().getMonth();
if (monthDiff != 0) {
return monthDiff;
}
return e1.getBirthday().getDay() - e2.getBirthday().getDay();
}
}
public class Main {
public static void main(String[] args) {
TreeSet<Employee> employeesByName = new TreeSet<>();
employeesByName.add(new Employee("John", 30, new MyDate(5, 25, 1991)));
employeesByName.add(new Employee("Alice", 25, new MyDate(3, 15, 1996)));
employeesByName.add(new Employee("Bob", 35, new MyDate(7, 4, 1986)));
employeesByName.add(new Employee("Mary", 28, new MyDate(9, 8, 1993)));
employeesByName.add(new Employee("David", 40, new MyDate(12, 31, 1981)));
System.out.println("Employees sorted by name:");
for (Employee employee : employeesByName) {
System.out.println(employee);
}
TreeSet<Employee> employeesByBirthday = new TreeSet<>(new BirthdayComparator());
employeesByBirthday.addAll(employeesByName);
System.out.println("Employees sorted by birthday:");
for (Employee employee : employeesByBirthday) {
System.out.println(employee);
}
}
}
```
输出结果为:
```
Employees sorted by name:
Employee{name='Alice', age=25, birthday=3/15/1996}
Employee{name='Bob', age=35, birthday=7/4/1986}
Employee{name='David', age=40, birthday=12/31/1981}
Employee{name='John', age=30, birthday=5/25/1991}
Employee{name='Mary', age=28, birthday=9/8/1993}
Employees sorted by birthday:
Employee{name='David', age=40, birthday=12/31/1981}
Employee{name='Bob', age=35, birthday=7/4/1986}
Employee{name='Alice', age=25, birthday=3/15/1996}
Employee{name='John', age=30, birthday=5/25/1991}
Employee{name='Mary', age=28, birthday=9/8/1993}
```
阅读全文