7-2 校园角色类设计(学生Student、教员Faculty和职员Staff)

简介: 7-2 校园角色类设计(学生Student、教员Faculty和职员Staff)

7-2 校园角色类设计(学生Student、教员Faculty和职员Staff)


分数 15

全屏浏览题目

切换布局

作者 刘凤良

单位 天津仁爱学院


学校需要构建综合系统,使用者包含多种角色。角色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班。


持续关注 有更多题目

import java.time.LocalDate;
 
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 {
    private String employeeID;
    private LocalDate hireDate;
    private String title;
 
    public Faculty(String name, int age, String employeeID, int year, int month, int day, String title) {
        super(name, age);
        this.employeeID = employeeID;
        this.hireDate = LocalDate.of(year, month, day);
        this.title = title;
    }
 
    @Override
    public void show() {
        super.show();
        System.out.println("工号是" + employeeID + "," + hireDate.getYear() + "年" + hireDate.getMonthValue() + "月" + hireDate.getDayOfMonth() + "日入职。是一名教师," + title + "职称。");
    }
}
 
//派生类 Student 学生
class Student extends Role {
    private String studentID;
    private String className;
 
    public Student(String name, int age, String studentID, String className) {
        super(name, age);
        this.studentID = studentID;
        this.className = className;
    }
 
    @Override
    public void show() {
        super.show();
        System.out.println("学号是" + studentID + ",来自" + className + "班。");
    }
}
 
//派生类 Staff 职员
class Staff extends Role {
    private String employeeID;
    private LocalDate hireDate;
    private String position;
 
    public Staff(String name, int age, String employeeID, int year, int month, int day, String position) {
        super(name, age);
        this.employeeID = employeeID;
        this.hireDate = LocalDate.of(year, month, day);
        this.position = position;
    }
 
    @Override
    public void show() {
        super.show();
        System.out.println("工号是" + employeeID + "," + hireDate.getYear() + "年" + hireDate.getMonthValue() + "月" + hireDate.getDayOfMonth() + "日入职。是一名" + position + "。");
    }
}
相关文章
|
存储 物联网 Java
Java面向对象程序设计综合练习2(编程题)(下)
Java面向对象程序设计综合练习2(编程题)(下)
862 0
|
3天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8095 2
|
12天前
|
域名解析 人工智能 Linux
|
20天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10638 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
6天前
|
人工智能 安全 网络安全
|
11天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1600 5
|
10天前
|
云安全 数据采集 人工智能
|
17天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2757 8