酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java实现Person、Student、Employee、Company类及对象排序与分类

以下是实现该需求的 Java 代码: ```java import java.text.DecimalFormat; import java.util.*; // 定义 Person 抽象类 abstract class Person implements Comparable<Person> { String name; int age; boolean gender; public Person(String name, int age, boolean gender) { this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return name + "-" + age + "-" + gender; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Person person = (Person) obj; return age == person.age && gender == person.gender && (name == null ? person.name == null : name.equals(person.name)); } @Override public int compareTo(Person other) { int nameCompare = this.name.compareTo(other.name); if (nameCompare != 0) { return nameCompare; } return Integer.compare(this.age, other.age); } } // 定义 Student 类 class Student extends Person { String stuNo; String clazz; public Student(String name, int age, boolean gender, String stuNo, String clazz) { super(name, age, gender); this.stuNo = stuNo; this.clazz = clazz; } @Override public String toString() { return "Student:" + super.toString() + "-" + stuNo + "-" + clazz; } @Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; Student other = (Student) obj; return (stuNo == null ? other.stuNo == null : stuNo.equals(other.stuNo)) && (clazz == null ? other.clazz == null : clazz.equals(other.clazz)); } } // 定义 Company 类 class Company { String name; public Company(String name) { this.name = name; } @Override public String toString() { return name; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Company company = (Company) obj; return (name == null ? company.name == null : name.equals(company.name)); } } // 定义 Employee 类 class Employee extends Person { Company company; double salary; public Employee(String name, int age, boolean gender, double salary, Company company) { super(name, age, gender); this.salary = salary; this.company = company; } @Override public String toString() { DecimalFormat df = new DecimalFormat("#.#"); return "Employee:" + super.toString() + "-" + (company == null ? "null" : company.toString()) + "-" + df.format(salary); } @Override public boolean equals(Object obj) { if (!super.equals(obj)) return false; Employee other = (Employee) obj; DecimalFormat df = new DecimalFormat("#.#"); return (company == null ? other.company == null : company.equals(other.company)) && df.format(salary).equals(df.format(other.salary)); } } // 主类 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Person> personList = new ArrayList<>(); // 创建对象 while (scanner.hasNext()) { String type = scanner.next(); if (!type.equals("s") && !type.equals("e")) { break; } String name = scanner.next(); int age = scanner.nextInt(); boolean gender = scanner.nextBoolean(); if (type.equals("s")) { String stuNo = scanner.next(); String clazz = scanner.next(); if ("null".equals(clazz)) { clazz = null; } personList.add(new Student(name, age, gender, stuNo, clazz)); } else { double salary = scanner.nextDouble(); String companyName = scanner.next(); Company company = "null".equals(companyName) ? null : new Company(companyName); personList.add(new Employee(name, age, gender, salary, company)); } } // 对 personList 排序 Collections.sort(personList); // 输出排序后的 personList for (Person person : personList) { System.out.println(person); } // 接受输入 String input = scanner.next(); if ("exit".equals(input)) { return; } // 将 personList 中的元素按类型分别放到 stuList 与 empList List<Student> stuList = new ArrayList<>(); List<Employee> empList = new ArrayList<>(); for (Person person : personList) { if (person instanceof Student) { Student student = (Student) person; if (!contains(stuList, student)) { stuList.add(student); } } else if (person instanceof Employee) { Employee employee = (Employee) person; if (!contains(empList, employee)) { empList.add(employee); } } } // 输出 stuList System.out.println("stuList"); for (Student student : stuList) { System.out.println(student); } // 输出 empList System.out.println("empList"); for (Employee employee : empList) { System.out.println(employee); } } private static <T> boolean contains(List<T> list, T obj) { for (T item : list) { if (item.equals(obj)) { return true; } } return false; } } ``` ### 代码解释: 1. **Person 类**: - 包含 `name`、`age` 和 `gender` 属性。 - 实现了 `toString` 方法,返回 `name-age-gender` 格式的字符串。 - 实现了 `equals` 方法,比较 `name`、`age` 和 `gender`。 - 实现了 `Comparable<Person>` 接口,用于按照姓名升序排序,姓名相同则按年龄升序排序。 2. **Student 类**: - 继承自 `Person` 类,包含 `stuNo` 和 `clazz` 属性。 - 实现了 `toString` 方法,返回 `Student:person的toString-stuNo-clazz` 格式的字符串。 - 实现了 `equals` 方法,先调用父类的 `equals` 方法,若返回 `true`,则继续比较 `stuNo` 和 `clazz`。 3. **Company 类**: - 包含 `name` 属性。 - 实现了 `toString` 方法,直接返回 `name`。 - 实现了 `equals` 方法,比较 `name` 是否相同。 4. **Employee 类**: - 继承自 `Person` 类,包含 `company` 和 `salary` 属性。 - 实现了 `toString` 方法,返回 `Employee:person的toString-company-salary` 格式的字符串。 - 实现了 `equals` 方法,先调用父类的 `equals` 方法,若返回 `true`,则继续比较 `company` 和 `salary`,比较 `salary` 时保留 1 位小数。 5. **Main 类**: - 接受用户输入,根据输入创建 `Student` 或 `Employee` 对象,并将其放入 `personList` 中。 - 对 `personList` 进行排序。 - 接受用户输入,若输入为 `exit` 则退出程序。 - 将 `personList` 中的元素按类型分别放到 `stuList` 和 `empList` 中,避免放入相同的对象。 - 输出 `stuList` 和 `empList`。 ### 开发建议: - **代码复用**:在 `Student` 和 `Employee` 类的构造函数中,使用 `super` 关键字复用 `Person` 类的构造函数,提高代码的复用性。 - **equals 方法**:在 `equals` 方法中,要考虑 `null` 的情况,避免出现 `NullPointerException`。 - **排序**:使用 `Collections.sort` 方法对 `personList` 进行排序,提高代码的简洁性。 ######[AI写代码神器 | 2085点数解答 | 2026-05-10 21:37:29]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]