7-2 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company

张开发
2026/4/15 20:31:58 15 分钟阅读

分享文章

7-2 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company
字节出品必属精品。免费注册Coze解锁下一代生产力工具。定义Person抽象类Student类、Company类Employee类。Person类的属性String name, int age, boolean genderPerson类的方法:public Person(String name, int age, boolean gender); public String toString(); // 返回name-age-gender格式的字符串 public boolean equals(Object obj);// 比较name、age、gender,都相同返回true,否则返回falseStudent类继承自Person属性:String stuNo, String clazzStudent类的方法:// 建议使用super复用Person类的相关有参构造函数 public Student(String name, int age, boolean gender, String stuNo, String clazz); public String toString(); // 返回 “Student:person的toString-stuNo-clazz”格式的字符串 public boolean equals(Object obj); // 首先调用父类的equals方法,如果返回true则继续比较stuNo与clazz。Employee类继承自Person属性Company company, double salaryEmployee类方法// 建议使用super复用Person类的相关有参构造函数 public Employee(String name, int age, boolean gender, double salary, Company company); public String toString(); //返回Employee:person的toString-company-salary格式的字符串 public boolean equals(Object obj);//首先调用父类的equals方法,如果返回true。再比较company与salary。 // 比较salary属性时使用DecimalFormat df new DecimalFormat(#.#);保留1位小数编写equals方法重要说明对Employee的company属性的比较。要考虑传入为null的情况。如果company不为null且传入为null返回false对所有String字符类型比较时也要考虑null情况。提示排序可使用Collections.sortequals方法要考虑周全main方法说明创建若干Student对象、Employee对象。输入s然后依次输入name age gender stuNo clazz创建Student对象。输入e然后依次输入name age gender salary company创建Employee对象。然后将创建好的对象放入ListPerson personList。输入其他字符则结束创建。说明:对于String类型如果为null则不创建对象而赋值为null。对于company属性如果为null则赋值为null否则创建相应的Company对象。对personList中的元素实现先按照姓名升序排序姓名相同再按照年龄升序排序。提示可使用ComparablePerson或ComparatorPerson接受输入如果输入为exit则return退出程序否则继续下面步骤。将personList中的元素按照类型分别放到stuList与empList。注意不要将两个内容相同的对象放入列表是否相同是根据equals返回结果进行判定。输出字符串stuList然后输出stuList中的每个对象。输出字符串empList然后输出empList中的每个对象。1-3为一个测试点4-6为一个测试点输入样例s zhang 23 false 001 net15 e wang 18 true 3000.51 IBM s zhang 23 false 001 net15 e bo 25 true 5000.51 IBM e bo 25 true 5000.52 IBM e bo 18 true 5000.54 IBM e tan 25 true 5000.56 IBM e tan 25 true 5000.51 IBM s wang 17 false 002 null s wang 17 false 002 null e hua 16 false 1000 null s wang 17 false 002 net16 e hua 16 false 1000 null e hua 18 false 1234 MicroSoft ! continue输出样例Employee:bo-18-true-IBM-5000.54 Employee:bo-25-true-IBM-5000.51 Employee:bo-25-true-IBM-5000.52 Employee:hua-16-false-null-1000.0 Employee:hua-16-false-null-1000.0 Employee:hua-18-false-MicroSoft-1234.0 Employee:tan-25-true-IBM-5000.56 Employee:tan-25-true-IBM-5000.51 Student:wang-17-false-002-null Student:wang-17-false-002-null Student:wang-17-false-002-net16 Employee:wang-18-true-IBM-3000.51 Student:zhang-23-false-001-net15 Student:zhang-23-false-001-net15 stuList Student:wang-17-false-002-null Student:wang-17-false-002-net16 Student:zhang-23-false-001-net15 empList Employee:bo-18-true-IBM-5000.54 Employee:bo-25-true-IBM-5000.51 Employee:hua-16-false-null-1000.0 Employee:hua-18-false-MicroSoft-1234.0 Employee:tan-25-true-IBM-5000.56 Employee:tan-25-true-IBM-5000.51 Employee:wang-18-true-IBM-3000.51AC代码import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; class Person { private String name; private int age; private boolean gender; private String str; Person(String name, int age, boolean gender, String str) { this.name name; this.age age; this.gender gender; this.str str; } public String toString() { return name - age - gender; } Override public boolean equals(Object obj) { Person aPerson (Person) obj; if (aPerson.name null || this.name null) { return false; } return aPerson.name.compareTo(this.name) 0 aPerson.age this.age aPerson.gender this.gender; } String getName() { return name; } int getAge() { return age; } String getStr() { return str; } } class Student extends Person { private String stuNo; private String clazz; Student(String name, int age, boolean gender, String str, String stuNo, String clazz) { super(name, age, gender, str); this.stuNo stuNo; this.clazz clazz; } public String toString() { return super.toString() - stuNo - clazz; } public boolean equals(Object obj) { Student a (Student) obj; if (super.equals(obj)) { if (this.stuNo null | this.clazz null || a.clazz null || a.stuNo null) { return false; } return this.clazz.compareTo(a.clazz) 0 this.stuNo.compareTo(a.stuNo) 0; } return false; } } class Company { private String name; 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) return false; if (getClass() ! obj.getClass()) return false; Company other (Company) obj; if (name null) { return other.name null; } else return name.equals(other.name); } } class Employee extends Person { private Company company; private double salary; Employee(String name, int age, boolean gender, String str, Company company, double salary) { super(name, age, gender, str); this.company company; this.salary salary; } Override public String toString() { return super.toString() - company.toString() - salary; } Override public boolean equals(Object obj) { if (super.equals(obj)) { Employee other (Employee) obj; if (this.company.toString() null || other.company.toString() null) { return false; } String df1 new DecimalFormat(#.#).format(this.salary); String df2 new DecimalFormat(#.#).format(other.salary); return this.company.toString().compareTo(other.company.toString()) 0 df1.compareTo(df2) 0; } return false; } } public class Main { /** * param args null * author 徐亮亮 */ public static void main(String[] args) { Scanner sc new Scanner(System.in); String c; String nameString; int age; boolean gender; ArrayListPerson persons new ArrayList(); ArrayListStudent students new ArrayList(); ArrayListEmployee employees new ArrayList(); String tempString; String stuNoString; String company; String clazzString; double salary; while (true) { c sc.next(); if (c.compareTo(s) 0) { nameString sc.next(); age sc.nextInt(); gender sc.nextBoolean(); stuNoString sc.next(); clazzString sc.next(); Student tempStudent new Student(nameString, age, gender, c, stuNoString, clazzString); persons.add(tempStudent); } else if (c.compareTo(e) 0) { nameString sc.next(); age sc.nextInt(); gender sc.nextBoolean(); salary sc.nextDouble(); company sc.next(); Company company2 new Company(company); Employee tempEmployee new Employee(nameString, age, gender, c, company2, salary); persons.add(tempEmployee); } else { persons.sort(Comparator.comparing(Person::getName).thenComparingInt(Person::getAge)); for (Person person : persons) { if (person.getStr().compareTo(s) 0) { System.out.println(Student: person.toString()); int flag 0; for (Student student : students) { if (student.equals(person)) { flag 1; break; } } if (flag 0) { students.add((Student) person); } } else { System.out.println(Employee: person.toString()); int flag 0; for (Employee employee : employees) { if (employee.equals(person)) { flag 1; break; } } if (flag 0) { employees.add((Employee) person); } } } tempString sc.next(); if (tempString.compareTo(exit) 0 || tempString.compareTo(return) 0) { return; } System.out.println(stuList); for (Student student : students) { System.out.println(Student: student.toString()); } System.out.println(empList); for (Employee employee : employees) { System.out.println(Employee: employee.toString()); } } } } }

更多文章