博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7-3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (30分)
阅读量:4035 次
发布时间:2019-05-24

本文共 7700 字,大约阅读时间需要 25 分钟。

定义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情况。

提示

排序可使用Collections.sortequals方法要考虑周全

main方法说明

创建若干Student对象、Employee对象。输入s,然后依次输入name age gender stuNo clazz创建Student对象。输入e,然后依次输入name age gender salary company创建Employee对象。然后将创建好的对象放入List
personList。输入其他字符,则结束创建。创建说明:对于String类型,如果为null则不创建对象,而赋值为null。对于company属性,如果为null则赋值为null,否则创建相应的Company对象。对personList中的元素实现先按照姓名升序排序,姓名相同再按照年龄升序排序。提示:可使用Comparable
或Comparator
接受输入,如果输入为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.51

import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner se = new Scanner(System.in); ArrayList
列表=new ArrayList
(); while (true) {
String s或e =se.next(); if(s或e.equals("s")) {
列表.add(new Student(se.next(),se.nextInt(),se.nextBoolean(),se.next(),se.next())); }else if(s或e.equals("e")) {
列表.add(new Employee(se.next(),se.nextInt(),se.nextBoolean(),se.nextDouble(),new Company(se.next()))); }else {
break; } } Collections.sort(列表, new 排序类()); for(Person 一个人:列表) {
if(一个人 instanceof Student) {
System.out.println(((Student)一个人).toString()); }else {
System.out.println(((Employee)一个人).toString()); } } String 输入exit或其他=se.next(); if(输入exit或其他.equals("exit")) {
return; }else {
ArrayList
stuList=new ArrayList
(); ArrayList
empList=new ArrayList
(); for(Person w:列表) { if(w instanceof Student) { if(!stuList.contains((Student)w)) { stuList.add((Student)w); } } if(w instanceof Employee) { if(!empList.contains((Employee)w)) { empList.add((Employee)w); } } } System.out.println("stuList"); for(Student a:stuList) { System.out.println(a.toString()); } System.out.println("empList"); for(Employee a:empList) { System.out.println(a.toString()); } } }}abstract class Person{ String name;int age; boolean gender; public Person(String name, int age, boolean gender) { super(); if(name.equals("null")) { this.name = null; }else { 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) 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; } }class Student extends Person{ String stuNo,clazz; public Student(String name, int age, boolean gender, String stuNo, String clazz) { super(name, age, gender); if(stuNo.equals("null")) { this.stuNo = null; }else { this.stuNo = stuNo; } if(clazz.equals("null")) { this.clazz = null; }else { this.clazz = clazz; } } @Override public String toString() { return "Student:" + super.toString()+"-"+ stuNo + "-" + clazz; } @Override public 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) { if(!name.equals("null")) { this.name = name; }else { this.name = null; } } @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) { 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, double salary, Company company) { super(name, age, gender); this.company = company; this.salary = salary; } @Override public String toString() { return "Employee:" +super.toString()+"-"+ company + "-" + salary ; } @Override public 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; String newpersalary = new DecimalFormat("#.#").format(other.salary); String newthissalary = new DecimalFormat("#.#").format(this.salary); if (company == null) { if (other.company != null) return false; } else if (!company.equals(other.company)) return false; if (!newpersalary.equals(newthissalary)) return false; return true; }}class 排序类 implements Comparator
{ @Override public int compare(Person o1, Person o2) { if(o1.name.compareTo(o2.name)>0) { return 1; }else if(o1.name.compareTo(o2.name)<0) { return -1; }else { if(o1.age>o2.age) { return 1; }else if(o1.age

转载地址:http://ihbdi.baihongyu.com/

你可能感兴趣的文章
java获取随机数
查看>>
url中传递中文参数时的转码与解码
查看>>
百度Ueditor设置允许上传的图片格式及大小
查看>>
java图形验证码生成工具类及web页面校验验证码
查看>>
微信菜单的配置
查看>>
移动端的emoji表情符号插入MySQL数据库失败
查看>>
mysql对指定列进行排名
查看>>
Eclipse安装FindBugs插件
查看>>
联想T440怎么把原装Win8或Win10换成Win7系统
查看>>
PowerDesigner将物理数据模型图生成图片
查看>>
PowerDesigner创建物理数据模型(PDM)
查看>>
PowerDesigner:导入SQL脚本
查看>>
PowerDesigner使用教程
查看>>
eclipse中使用Ctrl+Alt+↑或↓时屏幕旋转的问题
查看>>
freemarker 数字格式化(金额格式化)
查看>>
eclipse中Deployment Assembly选项设置说明
查看>>
maven项目报:Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
pom.xml中maven-compiler-plugin插件配置的使用
查看>>
使用maven-war-plugin 对Maven项目进行动态打包
查看>>
spring定时任务配置
查看>>