题目:【Person、Student、Employee类】(注:此题在书上原题基础上有修改)设计一个名为Person的类和它的两个名为Student和Employee子类。
每个人都有姓名和电话号码。学生有年级状态(大一、大二、大三或大四)。将这些状态定义为常量。一个雇员有工资和受聘日期。定义一个名为MyDate的类,包含数据域:year(年)、month(月)和day(日)。将各个类的数据域进行封装,并设置合理的读写访问器。
覆盖每个类中的toString方法,返回类名及相应的类中可以获取的所有信息构成的字符串,如Person类的toString方法返回“Person类:姓名为* 电话为”;Student类的toString方法返回“Student类: 姓名为 电话为 年级为**”。
在Student和Employee两个类中分别加入displayObject()方法,可以打印出对学生和雇员的提示消息,提示学生“to (学生姓名):请按时交实验报告”,提示雇员“to (雇员姓名):请按时上班”。
目标输出任务:
画出这些类的UML图。
实现这些类。
编写一个测试类,1)创建方法public static void m1(Person p),显示p的姓名;2)创建方法public static void m2(Person p),打印p的toString方法返回的字符串;3)创建方法public static void m3(Person p),如果p是Student类或者Employee类的实例,分别调用它们的displayObject();4)在主函数中创建Person、Student、Employee的对象实例,将它们均声明为Person类对象。将它们分别传入m1、m2和m3,体会多态。
//代码实现:
//Personl类
package shiyan51;
public class Person {
public String name;
public String PhoneNumber;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
PhoneNumber = phoneNumber;
}
public Person(String name,String PhoneNumber){
this.name=name;
this.PhoneNumber=PhoneNumber;
}
public String toString(){
return "姓名为:"+name+"电话为:"+PhoneNumber;
}
}
//Student类
package shiyan51;
public class Student extends Person {
public String Grade;
public final static String freshman ="大一";
public final static String sophomoreman="大二";
public final static String junior="大三";
public final static String senior="大四";
public String getGrade() {
return Grade;
}
public void setGrade(String Grade) {
this.Grade = Grade;
}
public Student(String name,String PhoneNumber,String Grade){
super(name,PhoneNumber);
this.Grade=Grade;
}
public void displayObject(){
System.out.println("to"+name+":请按时交实验报告");
}
public String toString(){
return super.toString()+"年级为:"+Grade;
}
}
//Employee类
package shiyan51;
public class Employee extends Person {
private double Salary;
Mydate mydate;
public int year;
public int month;
public int day;
public void MyDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
public Employee(String Name,String PhoneNumber,double Salary,Mydate date){
super(Name,PhoneNumber);
this.Salary=Salary;
mydate=date;
}
public double getSalary() {
return Salary;
}
public void setSalary(double salary) {
Salary = salary;
}
public void displayObject(){
System.out.println("to"+name+":请按时上班");
}
public Mydate getMydate() {
return mydate;
}
public void setMydate(Mydate mydate) {
this.mydate = mydate;
}
public String toString() {
return super.toString()+"工资:"+Salary+" "+mydate.year+" "+mydate.month+" "+mydate.getDay();
}
}
//Mydate类
package shiyan51;
public class Mydate {
public int year;
public int month;
public int day;
public Mydate(int year, int month, int day) {
this.year=year;
this.month=month;
this.day=day;
}
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void setDay(int day) {
this.day = day;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
}
//test类
package shiyan51;
public class test {
public final static String freshman ="大一";
public final static String sophomoreman="大二";
public final static String junior="大三";
public final static String senior="大四";
//创建三种方法输出目标
public static void m1(Person P){
System.out.println("name of person:"+P.getName());
}
public static void m2(Person P){
System.out.println(P.toString());
}
public static void m3(Person p){
if (p instanceof Student){
((Student)p).displayObject();
}else if(p instanceof Employee){
((Employee)p).displayObject();
}else{
System.out.println(p.getName()+":没有被定义为学生或者雇员!");
}
}//如果p是Student类或者Employee类的实例,分别调用它们的displayObject()
public static void main(String[] args){
Person p1 = new Student("morethansea","18373163669",freshman);
Person p2 = new Employee("Steven","18354643537",2000,new Mydate(2012,12,2));
Person p3 = new Person("Curry","14434643537");
m2(p1);
m2(p2);
m2(p3);
m1(p1);
m1(p2);
m1(p3);
m3(p1);
m3(p2);
m3(p3);
}
}
//实验截图: