2、设计职员类它继承了Person类并组合了Date类。编写主程序显示职员数据如下:姓名:杨萍出生日期: 1988106性别:女工作部门:团委职务:团委书记工资:6000要求:画出类之间的继承关系图;写出类的设计思路;完整代码。
类之间的继承关系图如下:
+--------+
| Person |
+--------+
|
|
|
+--------+
| Date |
+--------+
|
|
|
+--------+
| Employee |
+--------+
类的设计思路:
- Person类:包括姓名、出生日期、性别三个属性,以及对这些属性的get和set方法。
- Date类:包括年、月、日三个属性,以及对这些属性的get和set方法。
- Employee类:继承了Person类,组合了Date类,包括工作部门、职务、工资三个属性,以及对这些属性的get和set方法。
完整代码如下:
class Person {
private String name;
private Date birth;
private String gender;
public Person(String name, Date birth, String gender) {
this.name = name;
this.birth = birth;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
class Date {
private int year;
private int month;
private int day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
class Employee extends Person {
private String department;
private String position;
private double salary;
private Date joinDate;
public Employee(String name, Date birth, String gender, String department, String position, double salary, Date joinDate) {
super(name, birth, gender);
this.department = department;
this.position = position;
this.salary = salary;
this.joinDate = joinDate;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Date birth = new Date(1988, 10, 6);
Date joinDate = new Date(2010, 9, 1);
Employee employee = new Employee("杨萍", birth, "女", "团委", "团委书记", 6000, joinDate);
System.out.println("姓名:" + employee.getName());
System.out.println("出生日期:" + employee.getBirth().getYear() + "." + employee.getBirth().getMonth() + "." + employee.getBirth().getDay());
System.out.println("性别:" + employee.getGender());
System.out.println("工作部门:" + employee.getDepartment());
System.out.println("职务:" + employee.getPosition());
System.out.println("工资:" + employee.getSalary());
}
}
``
原文地址: https://www.cveoy.top/t/topic/dpYR 著作权归作者所有。请勿转载和采集!