java现有Empolyee类,有属性成员( name, hiredate age salary) ,请定义成员的方法和构造方法等方法,并定义若干职员,信息如下所示,请编程实现使得员工的入 (需要通过程序实现排序,不能只使用人工排序输出哦)
时间: 2024-02-21 12:00:26 浏览: 57
好的,以下是Java实现:
首先是Employee类的定义,包括属性成员和相应的构造方法和getter方法:
```java
public class Employee {
private String name;
private String hiredate;
private int age;
private double salary;
public Employee(String name, String hiredate, int age, double salary) {
this.name = name;
this.hiredate = hiredate;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public String getHiredate() {
return hiredate;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
}
```
然后,我们可以定义若干个职员,以便进行排序。下面是一个示例:
```java
Employee e1 = new Employee("Alice", "2020-01-01", 25, 5000);
Employee e2 = new Employee("Bob", "2019-01-01", 26, 6000);
Employee e3 = new Employee("Charlie", "2018-01-01", 27, 7000);
Employee e4 = new Employee("David", "2017-01-01", 28, 8000);
Employee e5 = new Employee("Emma", "2016-01-01", 29, 9000);
```
接下来,我们可以使用Java中的Collections.sort()方法进行排序。由于我们需要按照工资从高到低进行排序,因此我们可以使用Comparator接口来指定排序的关键字,代码如下:
```java
List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);
Collections.sort(employees, new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
return Double.compare(e2.getSalary(), e1.getSalary());
}
});
```
最后,我们可以遍历排序后的职员列表,并输出各个职员的信息,代码如下:
```java
for (Employee e : employees) {
System.out.println("Name: " + e.getName() + ", Hiredate: " + e.getHiredate() + ", Age: " + e.getAge() + ", Salary: " + e.getSalary());
}
```
这样,我们就完成了按照工资从高到低进行排序的需求。
阅读全文