Java——继承中的构造方法

(仅记录自己的学习之路)
本题目要求编程实现5个类,具体要求如下:

  • 设计一个名为Person的类及其两个名为学生类Student和雇员类Employee的子类,每个人都有姓名、地址、电话号码和电子邮件地址。
  • Employee类又有子类:教员类Faculty和职员类Staff。
  • 学生有班级状态,为大一、大二、大三、大四(可定义为int类型)。
  • 一个雇员涉及办公室、工资和受聘日期,受聘日期的类型参考选择题里面的MyDate类。
  • 教员有办公时间和级别。
  • 职员有头衔。
  • 重写每个类的toString()方法,显示相应的对象的信息。

要求:
(1)画出这些类的UML图,UML类图中可以省略成员变量的getXXX()和setXXX()方法。
(2)实现上述各个类,并编写测试类测试这些类的toString()方法。
代码如下:

package TestExtends;

class Person{
	private String name;
	private String address;
	private String phone;
	private String email;
	public Person() {
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Person(String name, String address) {
		this.name = name;
		this.address = address;
	}
	public Person(String name, String address, String phone, String email) {
		this(name, address);
		this.phone = phone;
		this.email = email;
	}
	public String toString() {
		return "姓名:" + name + '\n' + "住址:" + address + '\n' + "电话:" + phone + '\n' + "邮箱:" + email + '\n';
	}
}

class Student extends Person{
	private int grade;
	public Student(){
	}
	public Student(String n, String a, String p, String e, int grade){
		super(n, a, p, e);
		this.grade = grade;
	}
	public String toString() {
		return super.toString() + "年级:" + grade + '\n';
	}
}

class MyDate{
	private int year;
	private int month;
	private int day;
	public MyDate() {
	}
	public MyDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	public String toString() {
		return year + "年" + month + "月" + day + "日";
	}
}

class Employee extends Person{
	private String office;
	private double salary;
	private MyDate date;
	public Employee() {
	}
	public String getOffice() {
		return office;
	}
	public void setOffice(String office) {
		this.office = office;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public MyDate getDate() {
		return date;
	}
	public void setDate(MyDate date) {
		this.date = date;
	}
	public Employee(String n, String a, String p, String e, String office, double salary, MyDate date) {
		super(n, a, p, e);
		this.office = office;
		this.salary = salary;
		this.date = date;
	}
	public String toString() {
		return super.toString() + "办公室:" + office + '\n' + "工资:" + salary + '\n' + "受聘日期:" + date.toString() + '\n';
	}
}

class Faculty extends Employee{
	private String office_hours;
	private int level;
	public Faculty() {
	}
	public String getOffice_hours() {
		return office_hours;
	}
	public void setOffice_hours(String office_hours) {
		this.office_hours = office_hours;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public Faculty(String n, String a, String p, String e, String o, double s, MyDate d, String office_hours, int level) {
		super(n, a, p, e, o, s, d);
		this.office_hours = office_hours;
		this.level = level;
	}
	public String toString() {
		return super.toString() + "办公时间:" + office_hours + '\n' +"级别:" + level + '\n';
	}
}

class Staff extends Employee{
	private String title;
	public Staff() {
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Staff(String n, String a, String p, String e, String o, double s, MyDate d, String title) {
		super(n, a, p, e, o, s, d);
		this.title = title;
	}
	public String toString() {
		return super.toString() + "头衔:" + title + '\n';
	}
}

public class TestExtends {
	public static void main(String[] args) {
		Person p = new Person();
		p.setName("赵钱");
		p.setAddress("北京市朝阳区");
		p.setPhone("12345678901");
		p.setEmail("[email protected]");
		System.out.println(p.toString());
		System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
		
		Student s = new Student("孙李", "北京市海淀区", "13456789012","[email protected]", 1);
		System.out.println(s.toString());
		System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
		
		Employee e = new Employee();
		e.setName("周吴");
		e.setAddress("北京市西城区");
		e.setPhone("14567893201");
		e.setEmail("[email protected]");
		e.setOffice("网工系办公室");
		e.setSalary(7500);
		e.setDate(new MyDate(2000,11,8));
		System.out.println(e.toString());
		System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
		
		Faculty f = new Faculty("郑王", "北京市东城区", "15678943210", "[email protected]", "计科系办公室", 8000, new MyDate(1999,9,21), "9:00—17:00", 1);
		System.out.println(f.toString());
		System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");
		
		Staff sf = new Staff("冯陈", "北京市朝阳区", "16789543210", "[email protected]", "软工系办公室", 9000, new MyDate(2000,11,28), "管理员");
		System.out.println(sf.toString());
	}
}

运行示例:
Java——继承中的构造方法_第1张图片
Java——继承中的构造方法_第2张图片
UML图:
Java——继承中的构造方法_第3张图片

你可能感兴趣的:(Java——继承中的构造方法)