学校需要构建综合系统,使用者包含多种角色。角色Role分两类:学生Student和雇员Employee;雇员又分为教员Faculty和职员Staff。
每个角色都有姓名、年龄。学生有学号、班级。一个雇员有工号、入职日期。教员有职称。职员有职位称号。
请以如下Main类为基础,构建各个角色类,将代码补充完整。
public class Main {
public static void main(String[] args) {
Faculty fac = new Faculty("张三", 32, "33006", 2021, 9, 1, "讲师");
Student stu = new Student("李四", 19, "20201103", "202011");
Staff sta = new Staff("王五", 27, "32011", 2017, 7, 23, "教务员");
fac.show();
sta.show();
stu.show();
}
}
//基类 Role
class Role {
protected String name; //姓名
protected int age;
//构造方法
public Role() {
}
public Role(String name, int age) {
this.name = name;
this.age=age;
}
//Setter/Getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//业务方法
public void show(){
System.out.print("我是"+name+",年龄"+age+"岁。");
}
}
//派生类 Faculty 教员
class Faculty extends Role{
}
//派生类 Student 学生
class Student extends Role{
}
//派生类 Staff 职员
class Staff extends Role{
}
我是张三,年龄32岁。工号是33006,2021年9月1日入职。是一名教师,讲师职称。
我是王五,年龄27岁。工号是32011,2017年7月23日入职。是一名教务员。
我是李四,年龄19岁。学号是20201103,来自202011班。
主要是分析结构。因为参数要和Main函数传入的类型一致。
其次需要记住继承只能有一次。子类不能再被继承,否则会出现异常。
由于是在实例化过程中直接用构造方法传入参数,因此不必重写Set和Get方法。
题目中容易出错的地方是学号和工号是String格式。
public class Main {
public static void main(String[] args) {
Faculty fac = new Faculty("张三", 32, "33006", 2021, 9, 1, "讲师");
Student stu = new Student("李四", 19, "20201103", "202011");
Staff sta = new Staff("王五", 27, "32011", 2017, 7, 23, "教务员");
fac.show();
sta.show();
stu.show();
}
}
//基类 Role
class Role {
String name; //姓名
int age;
//构造方法
public Role() {
}
public Role(String name, int age) {
this.name = name;
this.age = age;
}
//Setter/Getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//业务方法
public void show() {
System.out.println("我是" + name + ",年龄" + age + "岁。");
}
}
//派生类 Faculty 教员
class Faculty extends Role {
//入职时间
protected int Y;
protected int M;
protected int D;
protected String num; //工号
protected String zc;
public Faculty(String name, int age, String num, int Y, int M, int D, String zc) {
this.name = name;
this.age = age;
this.Y = Y;
this.D = D;
this.M = M;
this.zc = zc;
this.num = num;
}
public void show() {
System.out.println("我是" + name + ",年龄" + age + "岁。工号是" + num + "," + Y + "年" + M + "月" + D + "日入职。是一名教师," + zc + "职称。");
}
}
//派生类 Student 学生
class Student extends Role {
protected String num; //学号
protected String classnum; //班级
public Student(String name, int age, String num, String classnum) {
this.name = name;
this.age = age;
this.classnum = classnum;
this.num = num;
}
public void show() {
System.out.println("我是" + name + ",年龄" + age + "岁。学号是" + num + ",来自" + classnum + "班。");
}
}
//派生类 Staff 职员
class Staff extends Role {
protected int Y;
protected int M;
protected int D;
protected String num; //工号
protected String zc;
public Staff(String name, int age, String num, int Y, int M, int D, String zc) {
this.name = name;
this.age = age;
this.Y = Y;
this.D = D;
this.M = M;
this.zc = zc;
this.num = num;
}
public void show() {
System.out.println("我是" + name + ",年龄" + age + "岁。工号是" + num + "," + Y + "年" + M + "月" + D + "日入职。是一名" + zc + "。");
}
}
总结:
考察继承和构造方法、this关键字运用。