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

本文介绍了如何在Java中定义抽象类Person及其子类Student、Company和Employee,包括构造方法、toString方法和equals方法的实现,以及如何使用Comparator进行排序和区分不同类型的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义Person抽象类,Student类、Company类,Employee类。

Person类的属性String name, int age, boolean gender
Person类的方法:

public Person(String name, int age, boolean gender);
public String toString();         //返回"name-age-gender"格式的字符串
public boolean equals(Object obj);//比较name、age、gender,都相同返回true,否则返回false

Student类继承自Person,属性:String stuNo, String clazz
Student类的方法:

//建议使用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。

Company类属性:String name
Company类方法:

public Company(String name);
public String toString();         //直接返回name
public boolean equals(Object obj);//name相同返回true

Employee类继承自Person,属性:Company company, double salary
Employee类方法:

//建议使用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方法重要说明:

  1. 对Employee的company属性的比较。要考虑传入为null的情况。如果company不为null且传入为null,返回false
  2. 对所有String字符类型比较时,也要考虑null情况。

提示

  1. 排序可使用Collections.sort
  2. equals方法要考虑周全

main方法说明

  1. 创建若干Student对象、Employee对象。
    输入s,然后依次输入name age gender stuNo clazz创建Student对象
    输入e,然后依次输入name age gender salary company创建Employee对象
    然后将创建好的对象放入List<Person> personList。输入其他字符,则结束创建。

创建说明: 对于String类型,如果为null则不创建对象,而赋值为null。对于company属性,如果为null则赋值为null,否则创建相应的Company对象。

  1. 对personList中的元素实现先按照姓名升序排序,姓名相同再按照年龄升序排序。提示:可使用Comparable<Person>Comparator<Person>

  2. 接受输入,如果输入为exitreturn退出程序,否则继续下面步骤。

  3. 将personList中的元素按照类型分别放到stuList与empList。注意:不要将两个内容相同的对象放入列表(是否相同是根据equals返回结果进行判定)。

  4. 输出字符串stuList,然后输出stuList中的每个对象。

  5. 输出字符串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.51

代码展示

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Person> list = new ArrayList<>();
        while (true) {
            String choice = sc.next();
            if (choice.equals("s")) {
                String name = sc.next();
                int age = sc.nextInt();
                boolean gender = sc.nextBoolean();
                String stuNo = sc.next();
                String clazz = sc.next();
                Student s = new Student(name, age, gender, stuNo, clazz);
                list.add(s);
            } else if (choice.equals("e")) {
                String name = sc.next();
                int age = sc.nextInt();
                boolean gender = sc.nextBoolean();
                double salary = sc.nextDouble();
                String company = sc.next();
                Employee e;
                if (company == null) {
                    e = new Employee(name, age, gender, null, salary);
                } else {
                    e = new Employee(name, age, gender, new Company(company), salary);
                }
                list.add(e);
            } else break;
        }
        Collections.sort(list, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                int x = p1.getName().compareTo(p2.getName());
                if (x == 0) {
                    return p1.getAge() - p2.getAge();
                }
                return x;
            }
        });

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }

        String nextStep = sc.next();
        if (nextStep.equals("exit")) return;

        ArrayList<Student> stuList = new ArrayList<>();
        ArrayList<Employee> empList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof Student) {
                Student s = (Student) list.get(i);
                int flag = 0;
                for (int j = 0; j < stuList.size(); j++) {
                    if (s.equals(stuList.get(j))) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) stuList.add(s);
            } else if (list.get(i) instanceof Employee) {
                Employee e = (Employee) list.get(i);
                int flag = 0;
                for (int j = 0; j < empList.size(); j++) {
                    if (e.equals(empList.get(j))) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) empList.add(e);
            }
        }
        System.out.println("stuList");
        for (int i = 0; i < stuList.size(); i++) {
            System.out.println(stuList.get(i).toString());
        }
        System.out.println("empList");
        for (int i = 0; i < empList.size(); i++) {
            System.out.println(empList.get(i).toString());
        }
    }
}

abstract class Person {
    private String name;
    private int age;
    private boolean gender;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public boolean isGender() {
        return gender;
    }

    public Person() {
    }

    public Person(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    //返回"name-age-gender"格式的字符串
    public String toString() {
        return name + "-" + age + "-" + gender;
    }

    //比较name、age、gender,都相同返回true,否则返回false
    public boolean equals(Object obj) {
        Person p = (Person) obj;
        if (p.name.equals(name) && p.age == age && p.gender == gender)
            return true;
        return false;
    }

}

class Student extends Person {
    private String stuNo;
    private String clazz;

    public Student() {
    }

    public Student(String stuNo, String clazz) {
        this.stuNo = stuNo;
        this.clazz = clazz;
    }

    public Student(String name, int age, boolean gender, String stuNo, String clazz) {
        super(name, age, gender);
        this.stuNo = stuNo;
        this.clazz = clazz;
    }

    //返回 “Student:person的toString-stuNo-clazz”格式的字符串
    @Override
    public String toString() {
        return "Student:" + super.toString() + "-" + stuNo + "-" + clazz;
    }

    //首先调用父类的equals方法,如果返回true,则继续比较stuNo与clazz。
    /*@Override
    public boolean equals(Object obj) {
        if (super.equals(obj)) {
            Student s = (Student) obj;
            if (s.stuNo.equals(stuNo) && s.clazz.equals(clazz))
                return true;
            return false;
        }
        return false;
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Student student = (Student) o;
        return Objects.equals(stuNo, student.stuNo) && Objects.equals(clazz, student.clazz);
    }

    @Override
    public int hashCode() {
        return Objects.hash(stuNo, clazz);
    }
}

class Company {
    private String name;

    public String getName() {
        return name;
    }

    public Company() {
    }

    public Company(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        return name.equals(((Company) obj).name);
    }
}

class Employee extends Person {
    Company company;
    double salary;

    //建议使用super复用Person类的相关有参构造函数

    public Employee(Company company, double salary) {
        this.company = company;
        this.salary = salary;
    }

    public Employee(String name, int age, boolean gender, Company company, double salary) {
        super(name, age, gender);
        this.company = company;
        this.salary = salary;
    }
    //返回"Employee:person的toString-company-salary"格式的字符串

    @Override
    public String toString() {
        return "Employee:" + super.toString() + "-" + company + "-" + salary;
    }

    //首先调用父类的equals方法,如果返回true。再比较company与salary。
    //比较salary属性时,使用DecimalFormat df = new DecimalFormat("#.#");
    // 保留1位小数
    /*@Override
    public boolean equals(Object obj) {
        if (super.equals(obj)) {
            Employee e = (Employee) obj;
            DecimalFormat df = new DecimalFormat("#.#");
            df.setRoundingMode(RoundingMode.DOWN);//截断
            String str1 = df.format(salary);
            String str2 = df.format(e.salary);
            if (str1.equals(str2)) {
                if (e.company == null && this.company == null)
                    return true;
                if (company != null&&e.company==null)
                    return false;
                if(company.equals(e))
                    return true;
            }
            return false;
        }
        return false;
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Employee employee = (Employee) o;
        DecimalFormat df = new DecimalFormat("#.#");
        String str1 = df.format(salary);
        String str2 = df.format(employee.salary);
        return str1.compareTo(str2) == 0 && Objects.equals(company, employee.company);
    }

    @Override
    public int hashCode() {
        return Objects.hash(company, salary);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值