第十一章第二题(Person、Student、Employee、Faculty和Staff类)(Person、Student、Employee、Faculty and Staff calss)
- 11.2(Person、Student、Employee、Faculty和Staff类)设计一个名为Person的类及其两个名为Student和Employee的子类。Employee类又有子类:教员类Faculty和职员类Staff。每个人都有姓名、地址、电话号码和电子邮件地址。学生有班级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员涉及办公室、工资和受聘日期。使用编程练习题10.14中定义的MyDate类为受聘日期创建一个对象。教员有办公时间和级别。职员有头衔。重写每个类中的toString方法,显示相应的类名和人名。
画出这些类的UML图并实现这些类。编写一个测试程序,创建Person、Studet、Employee、Faculty和Staff,并且调用他们的toString方法。
11.2(Person、Student、Employee、Faculty and Staff calss)Design a class named person and two subclasses named student and employee. Employee class has subclasses: Faculty class faculty and staff class. Everyone has a name, address, phone number and email address. Students have class status (freshmen, sophomores, juniors or seniors). Define these states as constants. An employee involves the office, salary and date of appointment. Use the mydate class defined in programming exercise 10.14 to create an object for the engagement date. Teachers have office hours and grades. The staff have titles. Override the toString method in each class to display the corresponding class name and person name.
Draw UML diagrams of these classes and implement them. Write a test program, create person, student, employee, faculty and staff, and call their toString method. - 参考代码:
package chapter11;
import java.util.Date;
public class Code_02 {
public static void main(String[] args) {
Person person = new Person("jxh","园","158990800","44196409@qq.com");
System.out.println(person.toString());
Student student = new Student("cxy","园","11111","22222",1);
System.out.println(student.toString());
Employee employee = new Employee("d","园","11111","22222","sss",1000,new Date());
System.out.println(employee.toString());
Faculty faculty = new Faculty("e","园","11111","22222","sss",1000,new Date(),new Date(),1);
System.out.println(faculty.toString());
Staff staff = new Staff("f","杏园","11111","22222","sss",1000,new Date(),"经理");
System.out.println(staff.toString());
}
}
class Person{
private String name;
private String address;
private String phoneNumber;
private String email_address;
Person(String name,String address,String phoneNumber,String email_address){
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email_address = email_address;
}
public String getThisName(){
return this.name;
}
public String toString(){
return ("Person " + this.name);
}
}
class Student extends Person{
final static int freshman = 1;
final static int senior = 2;
final static int junior = 3;
final static int graduate = 4;
private int studengStatus = 0;
Student(String name,String address,String phoneNumber,String email_address,int studengStatus){
super(name,address,phoneNumber,email_address);
this.studengStatus = studengStatus;
}
@Override
public String toString(){
return ("Student " + this.getThisName());
}
}
class Employee extends Person{
private String office;
private double salary;
private Date acceptDate;
Employee(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate){
super(name,address,phoneNumber,email_address);
this.office = office;
this.salary = salary;
this.acceptDate = acceptDate;
}
@Override
public String toString(){
return ("Employee " + this.getThisName());
}
}
class Faculty extends Employee{
private Date workDate;
private int level;
Faculty(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate,Date workDate,int level){
super(name,address,phoneNumber,email_address,office,salary,acceptDate);
this.level = level;
this.workDate = workDate;
}
@Override
public String toString(){
return ("Faculty " + this.getThisName());
}
}
class Staff extends Employee{
private String title;
Staff(String name,String address,String phoneNumber,String email_address,String office,double salary,Date acceptDate,String title){
super(name,address,phoneNumber,email_address,office,salary,acceptDate);
this.title = title;
}
@Override
public String toString(){
return ("Staff " + this.getThisName());
}
}
- 结果显示:
Person jxh
Student cxy
Employee d
Faculty e
Staff f
Process finished with exit code 0