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

题目概况:

定义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

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

题目分析

​ 该题的题意不难理解,总结来说就是设计四个类——Person, Student, Company, Employee 并在main方法中测试你的代码

提交情况

第一次提交


题目提示我注意格式,经过我长达晚上1个小时+早上半个小时的折磨后,找出了自己这道题目所存在的问题

  1. 发现main方法中,将List<Person> personList全部输出的代码位置应该放在 要求3 的前面


在这块语句块中,System.out.println(p)应该在sort后直接输出

  1. String与常量进行比较时,应该将常量放在前面,例如

    "exit"放在in.next()前面

  2. 每个类的equals()方法,最好都要考虑传入的对象为null(空引用)的情况

第二次提交

总结

1、客观描述

  1. 题目较大,每写完一个方法要确定自己已经实现了该方法的所有功能(特别是equals(), toString()
  2. 测试点0的注意格式,由于题目没有说明 将List<Person> personList排好序后需直接输出,导致我第一次写没有写在 if("exit".equals(in.next()))的前面,导致出错

2、主观描述

  1. 又臭又长,但不可否认能极好地锻炼对 extends 的掌握,加深了继承与多态的理解,顺便学习了List<> 容器,Collections.sort()的使用以及使用时所需实现的接口Comparable<>
  2. 题目对main方法的描述没有提到将List<Person> personList需直接进行输出,我直接化身成为暴躁的蒙古上单

3、好题,main方法没讲清楚!,main方法没讲清楚!,main方法没讲清楚!!!

本人源码:

package hello;import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);List<Person> personList = new ArrayList<>();OUT:do {String str = in.next();switch (str) {case "s": {String name, stuNo, clazz;int age; boolean gender;name = in.next();age = in.nextInt(); gender = in.nextBoolean();stuNo = in.next(); clazz = in.next();//控制输入为nullif("null".equals(name)) {name = null;}if("null".equals(stuNo)) {stuNo = null;}if("null".equals(clazz)) {clazz = null;}Student newStu = new Student(name, age, gender, stuNo, clazz);personList.add(newStu);break;}case "e": {String name;int age; boolean gender;double salary; Company company;name = in.next();age = in.nextInt(); gender = in.nextBoolean();salary = in.nextDouble();company = new Company(in.next());//控制输入为nullif("null".equals(company.toString())) {company = new Company(null);  //让company去指向一个null的新Company}//调用构造函数Employee newE = new Employee(name, age, gender, salary, company);personList.add(newE);break;}default:break OUT;}}while(true);//   2Collections.sort(personList);  //按姓名排序, 姓名一样按年龄排序for(Person p : personList) {System.out.println(p);}//     3if("exit".equals(in.next())) {return;    //如果输入为exit则return退出程序}//   4将personList中的元素按照类型分别放到stuList与empListList<Student> stuList = new ArrayList<>();List<Employee> empList = new ArrayList<>();for(Person o : personList) {// 是Student类且不包含if(o instanceof Student && !stuList.contains(o)) {stuList.add((Student) o);}else if (o instanceof Employee && !empList.contains(o)) {empList.add((Employee) o);}}// 5 6System.out.println("stuList");for(Student s : stuList) {System.out.println(s);}System.out.println("empList");for(Employee e : empList) {System.out.println(e);}in.close();}
}abstract class Person implements Comparable<Person>{private String name;private int age;private boolean gender;public Person(String name, int age, boolean gender) {this.name = name;this.age = age;this.gender = gender;}public String toString()  {//返回"name-age-gender"格式的字符串 return name+"-"+age+"-"+gender;}public boolean equals(Object obj) {//比较name、age、gender,都相同返回true,否则返回falsePerson other = (Person)obj;if(name.equals(other.name) && age == other.age && gender == other.gender) {return true;}else {return false;}}@Overridepublic int compareTo(Person o) {int ret;//比较字符串if(name == o.name) {ret = 0;} else {ret = name.compareTo(o.name);}if(ret == 0) {Integer o1 = age;Integer o2 = o.age;ret = o1.compareTo(o2);}return ret;}
}class Student extends Person{private String stuNo;private String clazz;//建议使用super复用Person类的相关有参构造函数public Student(String name, int age, boolean gender, String stuNo, String clazz) {super(name, age, gender);this.stuNo = stuNo;this.clazz = clazz;}public String toString() {//返回 “Student:person的toString-stuNo-clazz”格式的字符串return "Student:"+super.toString()+"-"+stuNo+"-"+clazz;}public boolean equals(Object obj) {//首先调用父类的equals方法,如果返回true,则继续比较stuNo与clazz。Student other = (Student)obj;if(!super.equals(obj) ) {return false;}// 比较stuNoboolean ret1 = true;if(stuNo == other.stuNo) {} else if (stuNo == null || other.stuNo == null) {ret1 = false;} else if (!stuNo.equals(other.stuNo)) {ret1 = false;}// 比较clazzboolean ret2 = true;if(clazz == other.clazz) {} else if (clazz == null || other.clazz == null) {ret2 = false;} else if (!clazz.equals(other.clazz)) {ret2 = false;}return ret1 && ret2;}}class Company {private String name;public Company(String name) {this.name = name;}public String toString() {//直接返回nameif(name == null) {return "null";} else {return name;}}public boolean equals(Object obj) {//name相同返回trueCompany other = (Company)obj;boolean ret = true;if(name == other.name) {} else if (name == null || other.name == null) {ret = false;} else if(!name.equals(other.name)) {ret = false;}return ret;}}class Employee extends Person {private Company company;private double salary;//建议使用super复用Person类的相关有参构造函数public Employee(String name, int age, boolean gender, double salary, Company company) {super(name, age, gender);this.salary = salary;this.company = company;}public String toString() {//返回"Employee:person的toString-company-salary"格式的字符串// 当company为null,toString异常了,????// 原因:调用构造函数时,本意是想让name = null, 却使得company = nullreturn "Employee:"+super.toString()+"-"+company.toString()+"-"+salary;}public boolean equals(Object obj) {//首先调用父类的equals方法,如果返回true。再比较company与salary。//比较salary属性时,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数Employee other = (Employee)obj;    //先将传入的Object转为需要操作的类// 比较父类的属性 可调用父类的equals方法boolean ret = true;if(!super.equals(obj)) {ret = false;}//  比较自己的company 调用Company类里的equals方法if(company == null && other.company == null) {// 让程序往下继续判断} else if (company == null || other.company == null) {ret = false;} else if(!company.equals(other.company)) {ret = false;}//   比较salaryDecimalFormat df = new DecimalFormat("#.#");if(!(df.format(salary)).equals(df.format(other.salary))) {ret = false;}return ret;}
}

4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company**相关推荐

  1. java之面向对象:继承extends、super、覆盖override的用法

    继承的好处: 1 )提高了代码的复用性. 2 )让类与类之间产生了关系,给第三个特征多态提供 了前提. java支持单继承,不直接支持多继承,但对C++中的多继承机制进行改良. 单继承:一个子类只能有 ...

  2. java继承stu继承person_4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分)中的一些问题...

    1.Employee类的equals 由于题目要求//首先调用父类的equals方法,如果返回true.再比较company与salary. //比较salary属性时,使用DecimalFormat ...

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

    定义Person抽象类,Student类.Company类,Employee类. Person类的属性:String name, int age, boolean gender Person类的方法: ...

  4. 7-6 jmu-Java-03面向对象-06-继承覆盖综合练习

    写之前还是要先明确好程序编写的要求,以免过了样例但代码运行逻辑和要求不符合. 记录一个比较浮点型数据的方法 String df1 = new DecimalFormat("#.#" ...

  5. Java(10)面向对象之继承,内部类

    01 面向对象之继承 public class Inheritance02 {public class Person {/*** 昵称*/private String nickname;/*** 性别 ...

  6. 【JavaSE学习】03面向对象Java语法

    JavaSE(B站黑马)学习笔记 01Java入门 02数组.方法 03面向对象&Java语法 04-1Java高级(Stream流.异常处理.日志技术) 04-2Java高级(文件处理-IO ...

  7. java用继承编写宠物乐园_MoreThanJavaDay 5:面向对象进阶继承详解

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  8. Java基础-面向对象第二特征之继承(Inheritance)

    Java基础-面向对象第二特征之继承(Inheritance) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.继承的概述 在现实生活中,继承一般指的是子女继承父辈的财产.在程序 ...

  9. Java面向对象(继承、抽象类)

    面向对象 今日内容介绍 u 继承 u 抽象类 第1章 继承 1.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成 ...

最新文章

  1. 适合写python的电脑_这篇写给想选计算机专业的学弟学妹们
  2. 网页爬虫 python-Python爬虫解析网页的4种方式
  3. 网上书店例子(JSP和JavaBean)
  4. 给 Android 开发人员的 RxJava 具体解释
  5. 【转】逆变与协变详解
  6. 高低层特征融合【转载】
  7. 【Python成长之路】python 基础篇 -- global/nonlocal关键字使用
  8. 初识弹性文件服务:可靠的共享文件存储
  9. vue读取外部配置文件
  10. 用python写一个程序来验证每个数字的生成概率是否相同_Python实现简单生成验证码功能【基于random模块】...
  11. Azure KUDU工具
  12. VSCode 插件离线安装方法(转载)
  13. 抖音服务器升级维护时间,抖音服务器升级要多久2021
  14. steam遇到错误代码解决方案
  15. usnews计算机专业排名2018,2018USNEWS计算机专业TOP50院校及官网地址
  16. 前端javascript如何分享内容到twitter和Email
  17. 机器学习笔记1.矩估计、极大似然估计。
  18. html在线人数统计代码,网页在线人数统计的代码
  19. python实现2000投影坐标转经纬度
  20. windows 10 内置 OpenSSH客户端

热门文章

  1. [技巧]QQ密技(一)
  2. Go学习——runtime.Caller()函数
  3. combo 技术简单介绍
  4. DSPC6657读取图片数据并进行图像处理
  5. C基础 工程中常用的排序
  6. 信号完整性可能遇见的五类问题
  7. mysql cbrt函数_PostgreSQL学习笔记5之函数和操作符一
  8. Linux设备驱动开发详解 第3版 (即 Linux设备驱动开发详解 基于最新的Linux 4 0内核 )前言
  9. 将word转换html格式的文件,word 保存成 html格式文件
  10. 汉语属于哪个语系_汉语,日语,韩语分别属于什么语系?