| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package exericse.exericse02;
- import java.util.Objects;
- /**
- * @author WanJl
- * @version 1.0
- * @title Employee
- * @description
- * @create 2026/8/3
- */
- public class Employee implements Comparable<Employee> {
- private String id; // 工号
- private String name; // 姓名
- private double salary; // 薪资
- private String dept; // 部门
- public Employee(String id, String name, double salary, String dept) {
- this.id = id;
- this.name = name;
- this.salary = salary;
- this.dept = dept;
- }
- // TODO: 补全 getter / setter / toString 方法
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getSalary() {
- return salary;
- }
- public void setSalary(double salary) {
- this.salary = salary;
- }
- public String getDept() {
- return dept;
- }
- public void setDept(String dept) {
- this.dept = dept;
- }
- // TODO: 重写 compareTo —— 按薪资降序,薪资相同再按工号
- @Override
- public int compareTo(Employee o) {
- // 提示:Double.compare(o.getSalary(), this.salary) // 降序
- // 若返回 0,再比较工号:this.id.compareTo(o.getId())
- int result= Double.compare(o.getSalary(), this.salary);
- return result==0?this.id.compareTo(o.getId()):result;
- }
- @Override
- public boolean equals(Object o) {
- if (o == null || getClass() != o.getClass()) return false;
- Employee employee = (Employee) o;
- return Double.compare(salary, employee.salary) == 0 && Objects.equals(id, employee.id) && Objects.equals(name, employee.name) && Objects.equals(dept, employee.dept);
- }
- @Override
- public int hashCode() {
- return Objects.hash(id, name, salary, dept);
- }
- @Override
- public String toString() {
- return "Employee{" +
- "id='" + id + '\'' +
- ", name='" + name + '\'' +
- ", salary=" + salary +
- ", dept='" + dept + '\'' +
- '}';
- }
- }
|