4-5 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (15分)

本文主要是介绍4-5 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (15分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

定义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方法重要说明:

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

提示:

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

创建若干Student对象、Employee对象。
1.输入s,然后依次输入name age gender stuNo clazz创建Student对象。
输入e,然后依次输入name age gender salary company创建Employee对象。
然后将创建好的对象放入List<Person> personList。输入其他字符,则结束创建。
**创建说明:**对于String类型,如果为null则不创建对象,而赋值为null。对于company属性,如果为null则赋值为null,否则创建相应的Company对象。

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

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

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

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

6.输出字符串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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<Person> personList = new ArrayList<>();while(sc.hasNext()) {String select = sc.next(); // 输入s或者eif("s".equals(select)) {Student student = new Student(sc.next(), sc.nextInt(), sc.nextBoolean(), sc.next(), sc.next());personList.add(student);} else if("e".equals(select)) {String name = sc.next(); // 姓名int age = sc.nextInt(); // 年龄boolean b = sc.nextBoolean(); // 性别double salary = sc.nextDouble(); // 薪资Company company = new Company(sc.next()); // 公司Employee employee = new Employee(name, age, b, company, salary);personList.add(employee);} else {break;}}// 排序Collections.sort(personList);for (Person person : personList) {System.out.println(person);}String select = sc.next();if("exit".equals(select)) {System.exit(0);}// 输出员工,分别放到stuList和empListList<Student> stuList = new ArrayList<>();List<Employee> empList = new ArrayList<>();for (Person person : personList) {if (person instanceof Student) {Student stu = (Student) person;if(!stuList.contains(stu))stuList.add(stu);}else if (person instanceof Employee) {Employee emp = (Employee) person;if(!empList.contains(emp))empList.add(emp);}}System.out.println("stuList");for (Student stu : stuList) {System.out.println(stu);}System.out.println("empList");for (Employee emp : empList) {System.out.println(emp);}}
}class Person implements Comparable<Person> {String name; int age; boolean gender;public Person(String name, int age, boolean gender) {super();this.name = name;this.age = age;this.gender = gender;}public String toString() {//返回"name-age-gender"格式的字符串return name + "-" + age + "-" + gender;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Person other = (Person) obj;if (age != other.age)return false;if (gender != other.gender)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}@Overridepublic int compareTo(Person o) {int name = this.name.compareTo(o.name);return name == 0 ? (this.age - o.age) : name;}}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;}//返回 “Student:person的toString-stuNo-clazz”格式的字符串public String toString() {return "Student:" + super.toString() + "-" + stuNo + "-" + clazz;}// 首先调用父类的equals方法,如果返回true,则继续比较stuNo与clazz。@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (!super.equals(obj))return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (clazz == null) {if (other.clazz != null)return false;} else if (!clazz.equals(other.clazz))return false;if (stuNo == null) {if (other.stuNo != null)return false;} else if (!stuNo.equals(other.stuNo))return false;return true;}
}class Company {String name;public Company(String name) {this.name = name;}//直接返回namepublic String toString() {return name;}@Overridepublic 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) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}
}class Employee extends Person {Company company; double 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"格式的字符串public String toString() {return "Employee:" + super.toString() + "-" + company + "-" + salary;}// 首先调用父类的equals方法,如果返回true。再比较company与salary。@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (!super.equals(obj))return false;if (getClass() != obj.getClass())return false;Employee other = (Employee) obj;if (company == null) {if (other.company != null)return false;} else if (!company.equals(other.company))return false;DecimalFormat df = new DecimalFormat("#.#");if (!df.format(salary).equals(df.format(other.salary)))return false;return true;}}

这篇关于4-5 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (15分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/219562

相关文章

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP