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

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(空引用)的情况

第二次提交

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aFsDxcsn-1663996123446)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220924125636945.png)]

总结

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需直接进行输出,我直接化身成为暴躁的蒙古上单

本人源码:

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();
				
				//控制输入为null
				if("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());
				//控制输入为null
				if("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);
		
		// 	2
		Collections.sort(personList);	//按姓名排序, 姓名一样按年龄排序
		for(Person p : personList) {
			System.out.println(p);
		}
		
		// 	3
		if("exit".equals(in.next())) {
			return;	//如果输入为exit则return退出程序
		}
		
		//	4将personList中的元素按照类型分别放到stuList与empList
		List<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 6
		System.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,否则返回false
		Person other = (Person)obj;
		if(name.equals(other.name) && age == other.age && gender == other.gender) {
			return true;
		}
		else {
			return false;
		}
	}
	
	@Override
	public 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;
		}
		
		//	比较stuNo
		boolean ret1 = true;
		if(stuNo == other.stuNo) {
			
		} else if (stuNo == null || other.stuNo == null) {
			ret1 = false;
		} else if (!stuNo.equals(other.stuNo)) {
			ret1 = false;
		}
		// 比较clazz
		boolean 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() {
		//直接返回name
		if(name == null) {
			return "null";
		} else {
			return name;
		}
	}
	public boolean equals(Object obj) {
		//name相同返回true
		Company 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 = null
		return "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;
		}
		//	比较salary
		DecimalFormat df = new DecimalFormat("#.#");
		if(!(df.format(salary)).equals(df.format(other.salary))) {
			ret = false;
		}
		return ret;
	}
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: "jmu-java-03面向对象-06-继承覆盖综合练习" 指的是一个练习题目,要求定义四个类:PersonStudentEmployeeCompany。其中,Person 类是父类,StudentEmployee继承Person 类,Company 类可能有多个 Employee 对象。这道题的目的是练习继承覆盖的使用方法。 ### 回答2: 面向对象编程中的继承是非常重要的概念,它可以让我们避免重复编码,提高代码的重用性和灵活性。在这个题目中,我们需要继承Person类,并创建一个Student类和一个Employee类。同时,我们还需要创建一个Company类来管理所有的Employee。 首先,我们需要在Person类中创建属性(name, age, gender),并定义构造函数来初始化它们。然后,我们可以创建一个Student类,并继承Person类,在其中添加新的属性及其相应的getter和setter方法。例如,我们可以添加一个grade字段来表示学生的班级。同样地,我们可以创建一个Employee类,并继承Person类,在其中添加新的属性及其相应的getter和setter方法。例如,我们可以添加一个salary字段来表示员工的工资。 接下来,我们需要在Company类中管理所有的Employee。我们可以使用ArrayList来存储Employee对象,并定义相应的addEmployee和removeEmployee方法来添加或删除Employee对象。同时,我们也可以实现计算公司总工资和打印员工信息的方法。 在继承覆盖方法方面,我们需要注意一个重要的概念:父类的方法可以被子类继承覆盖。例如,在Person类中定义了一个speak方法,我们可以在StudentEmployee类中重写这个方法,使得它们的行为不同。同时,我们也可以使用super关键字来调用父类的方法,以实现方法的增强。 最后,为了保证代码的可读性和可维护性,我们需要对类和方法进行适当的封装和注释。这可以帮助其他开发者更好地理解我们的代码,并且在后续的开发中,也可以更方便地进行扩展和修改。 综上所述,这个练习中涉及到了继承覆盖方法、ArrayList、封装和注释等多个面向对象编程的知识点。通过这个练习,我们可以更深入地理解面向对象编程的思想,并且提高我们的编程技巧和代码质量。 ### 回答3: 在面向对象编程中,继承覆盖是两个非常重要的概念。继承让一个类可以继承另一个类的属性和方法,而覆盖则让子类可以覆盖父类的方法,从而实现自己的特定功能。在这个综合练习中,我们需要创建一个Person类,并根据需要创建StudentEmployeeCompany类,以便练习继承覆盖。 首先,我们需要创建Person类,该类需要包含两个属性:姓名和年龄,并且需要有一个构造函数,以便创建Person对象时可以初始化这两个属性。Person类还应该有一个获取姓名的方法和一个获取年龄的方法。这些方法应该被所有的子类继承,并且应该被子类覆盖以实现自己的特定功能。 接下来,我们可以创建Student类,该类是Person类的子类,所以它可以继承Person类的属性和方法。但是学生还有一个额外的属性:学校。因此,我们需要为学生类添加一个新的属性,并添加一个构造函数来初始化这个新属性。学生类还需要覆盖Person类的获取姓名和获取年龄方法,并添加一个获取学校的方法。 然后,我们可以创建Employee类,这个类也是Person类的子类,它可以继承Person类的属性和方法。但是员工还有一些额外的属性,例如工作编号和薪水。因此,我们需要为Employee类添加这些新的属性,并添加一个构造函数来初始化这些新属性。Employee类还应该覆盖Person类的获取姓名和获取年龄方法,并添加一个获取工作编号和薪水的方法。 最后,我们可以创建一个Company类,这个类需要实现某些方法来管理公司的员工。因此,Company类应该有一个员工列表属性,并且应该有一个添加员工和移除员工的方法。这些方法需要在公司中添加或删除员工对象。此外,公司类还应该有一个方法来计算所有员工的总薪水。 综上所述,这个综合练习中涉及了面向对象编程的许多关键概念,例如继承覆盖和类的组合。通过实现这些类和方法,我们可以更好地理解这些概念,并掌握如何在实际的编程任务中应用这些概念。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值