Program:按照如下要求设计类:
(1)设计一个日期类,用于记录年、月、日,并提供对日期处理的常用方法。
(2)设计一个职工类,该职工类至少具有下面的属性:职工号,姓名,性别,生日,工作部门,参加工作时间,设计相应的方法。
(3)设计一个测试类,采用对象数组模拟一个拥有10个职工的单位。
以下是日期类和员工类的实现
Program:按照如下要求设计类: (1)设计一个日期类,用于记录年、月、日,并提供对日期处理的常用方法。 (2)设计一个职工类,该职工类至少具有下面的属性:职工号,姓名,性别,生日,工作部门,参加工作时间,设计相应的方法。 (3)设计一个测试类,采用对象数组模拟一个拥有10个职工的单位。 以下是日期类和员工类的实现 /* * Description:定义日期时间类 * * Written By:Cai * * Date Written:2017-10-09 * * * */ package entity; public class MyDate { private String year; //年 private String month; //月 private String day; //日 //定义构造方法 public MyDate() { } public MyDate(String year,String month,String day) { this.year = year; this.month = month; this.day = day; } //定义setter和getter方法 public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getDay() { return day; } public void setDay(String day) { this.day = day; } //覆写toString()方法 public String toString() { return this.year + "年" + this.month + "月" + this.day + "日"; } }
1 /* 2 * Description:定义职工类 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-09 7 * 8 * 9 * */ 10 11 package entity; 12 13 import entity.MyDate; 14 15 public class Worker { 16 17 String workerId; //职工号 18 String workerName; //职工姓名 19 String workerSex; //职工性别 20 String workerPartment; //职工部门 21 MyDate workerBirthday; //职工生日 22 MyDate workerBeginDate; //职工开始工作时间 23 24 25 //定义构造方法 26 public Worker() { 27 28 } 29 30 public Worker(String id,String name,String sex,String partment,MyDate birthday,MyDate beginDate) { 31 32 this.workerId = id; 33 this.workerName = name; 34 this.workerSex = sex; 35 this.workerPartment = partment; 36 this.workerBirthday = birthday; 37 this.workerBeginDate = beginDate; 38 } 39 40 41 //定义setter和getter方法 42 public String getWorkerId() { 43 return workerId; 44 } 45 46 public void setWorkerId(String workerId) { 47 this.workerId = workerId; 48 } 49 50 public String getWorkerName() { 51 return workerName; 52 } 53 54 public void setWorkerName(String workerName) { 55 this.workerName = workerName; 56 } 57 58 public String getWorkerSex() { 59 return workerSex; 60 } 61 62 public void setWorkerSex(String workerSex) { 63 this.workerSex = workerSex; 64 } 65 66 public String getWorkerPartment() { 67 return workerPartment; 68 } 69 70 public void setWorkerPartment(String workerPartment) { 71 this.workerPartment = workerPartment; 72 } 73 74 public MyDate getWorkerBirthday() { 75 return workerBirthday; 76 } 77 78 public void setWorkerBirthday(MyDate workerBirthday) { 79 this.workerBirthday = workerBirthday; 80 } 81 82 public MyDate getWorkerBeginDate() { 83 return workerBeginDate; 84 } 85 86 public void setWorkerBeginDate(MyDate workerBeginDate) { 87 this.workerBeginDate = workerBeginDate; 88 } 89 90 //覆写toString方法 91 @Override 92 public String toString() { 93 return "Worker [workerId=" + workerId + ", workerName=" + workerName 94 + ", workerSex=" + workerSex + ", workerPartment=" 95 + workerPartment + ", workerBirthday=" + workerBirthday 96 + ", workerBeginDate=" + workerBeginDate + "]"; 97 } 98 99 }
下面是测试类
1 /* 2 * Description:定义测试类,模拟十个职工的单位 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-10-09 7 * 8 * */ 9 10 package main; 11 12 import entity.*; 13 14 public class TestDemo { 15 16 public static void main(String args[]) { 17 18 Worker[] workers = new Worker[10]; //声明Worker类数组对象,大小为10 19 20 //为数组对象依次赋值 21 for( int i = 0; i < 10; i++ ) { 22 23 workers[i] = new Worker("00000" + i,"worker_" + i,"男","办公室" + i,new MyDate("2017","10",(i + 1) + "" ),new MyDate("2017","11","" + (i + 1) )); 24 } 25 26 //打印数组对象 27 display(workers); 28 29 } 30 31 //定义方法,打印Worker类的数组对象信息 32 public static void display(Worker[] workers) { 33 34 for( int i = 0; i < workers.length; i++ ) { 35 System.out.println( workers[i] ); 36 } 37 } 38 39 }