Java第二章作业第二题

该博客介绍了如何在Java中设计一个类层次结构,包括Person父类及其Student、Employee子类,以及Employee的Faculty和Staff子类。每个类都有特定的属性如ID、姓名、地址等,并覆盖了toString()方法。测试程序随机创建了8个对象并打印了它们的信息。
摘要由CSDN通过智能技术生成

设计一个包含5个类的Java程序,名为Person的父类有两个子类,学生类Student和员工类Employee。Employee类有两个子类,教师类Faculty和 职员类Staff。所有人都有编号ID、姓名、地址、电话号码和电子邮件地址。学生类Student有班级状态(大一、大二、大三或大四)。教师类Faculty有主讲课程、上课时间、专业信息。职员类Staff有职务、入职日期信息。覆盖每个类中的toString()方法,输出相应的类名、编号ID和姓名。

编写一个测试程序,随机创建8个Student、Faculty或Staff对象,放在一个数组中,依次调用他们的toString()方法显示信息。


package com.company;
/**
 * 〈功能简述〉<br>
 * 
 *  设计一个包含5个类的Java程序,
 *  名为Person的父类有两个子类,学生类Student和员工类Employee。
 *  Employee类有两个子类,教师类Faculty和 职员类Staff。
 *  所有人都有编号ID、姓名、地址、电话号码和电子邮件地址。
 *  学生类Student有班级状态(大一、大二、大三或大四)。
 *  教师类Faculty有主讲课程、上课时间、专业信息。
 *  职员类Staff有职务、入职日期信息。
 *  覆盖每个类中的toString()方法,输出相应的类名、编号ID和姓名。
 *  编写一个测试程序,随机创建8个Student、Faculty或Staff对象,放在一个数组中,依次调用他们的toString()方法显示信息。
 *   @author Zhangnuanxin
 *   @create 2020/9/24
 *   @since 1.0.0
 */


class Person
{
    int ID , phonenumber;
    //采用int类型定义ID编号和电话号码

    String address , email , name;
    //采用String类型定义地址,电子邮箱地址和名字

    Person(){};     //默认构造函数
    Person(int i , int p , String add , String e, String na)
    {
        this.ID = i;
        this.phonenumber = p;
        this.address = add;
        this.email = e;
        this.name = na;
    }
    /**
     * 父类PersonStudent的含参构造参数
     * 参数分别为int类型的ID号、int类型的电话号码
     * String类型的地址,电子邮箱地址和名字
     * 对这些变量进行初始化
     */

    public String toString()
    {
        System.out.println("Class name is:  " + Person.class.getSimpleName());
        System.out.println(this.name + "'s ID is: " + this.ID);
        System.out.println("The name is: " + this.name);
        return null;
    }
    /**
     * 父类Person中的toString()函数,
     * 利用getSimpleName()函数返回类名
     * 输出类名,ID和名字
     * 共三行
     * 返回一个空值:满足String类型的返回值要求
     */
}

class Student extends Person        //Student类,继承自Person
{
    String classstatus;
    //采用String类型定义变量课程状态

    Student(){};        //默认构造函数
    Student(int i, int p, String add, String e, String na,String cs)
    {
        super(i, p, add, e, na);
        this.classstatus = cs;
    }
    /**
     * Student的含参构造参数
     * 继承自父类Person的构造函数,外加Student类独有的班级状态
     * 参数分别为int类型的ID号、int类型的电话号码
     * String类型的地址,电子邮箱地址和名字还有班级状态
     * 对这些变量进行初始化
     */

    public String toString()
    {
        System.out.println("Class name: " + Person.class.getSimpleName() + " (Extends from Person)");
        System.out.println(this.name + "'s ID is: " + this.ID);
        System.out.println("The name is: " + this.name);
        return null;
    }
    /**
     * Student中的toString()函数重构,
     * 利用getSimpleName()函数返回类名
     * 输出类名,ID和名字
     * 共三行
     * 返回一个空值:满足String类型的返回值要求
     */
}

class Employee extends Person       //Employee类,继承自Person类
{
    Employee(){}; //默认构造函数
    Employee(int i, int p, String add, String e, String na)
    {
        super(i, p, add, e, na);
    }
    /**
     * Employee类的含参构造参数
     * 继承自父类Person的构造函数
     * 参数分别为int类型的ID号、int类型的电话号码
     * String类型的地址,电子邮箱地址和名字
     * 对这些变量进行初始化
     */

    public String toString()
    {
        System.out.println("Class name: " + Employee.class.getSimpleName() + "(Extends from Person)");
        System.out.println(this.name + "'s ID is: " + this.ID);
        System.out.println("The name is: " + this.name);
        return null;
    }
    /**
     * Employee类中的toString()函数重构,
     * 利用getSimpleName()函数返回类名
     * 输出类名,ID和名字
     * 共三行
     * 返回一个空值:满足String类型的返回值要求
     */
}

class Faculty extends Employee      //Faculty类,继承自Employee类
{
    String ClassName, ClassTime,Proinfo;
    //采用String类型定义变量主讲课程、上课时间和专业信息

    Faculty(){};        //默认构造函数
    Faculty(int i, int p, String add, String e, String na , String cn , String ct, String Pi)
    {
        super(i, p, add, e, na);
        this.ClassName = cn;
        this.ClassTime = ct;
        this.Proinfo = Pi;
    }
    /**
     * Faculty类的含参构造参数
     * 继承自父类Employee的构造函数
     * 参数分别为int类型的ID号、int类型的电话号码
     * String类型的地址,电子邮箱地址和名字
     * 还有Faculty类特有的String类型的主讲课程、上课时间和专业信息
     * 对这些变量进行初始化
     */

    public String toString()
    {
        System.out.println("Class name: " + Faculty.class.getSimpleName() + "(Extends from Person)");
        System.out.println(this.name + "'s ID is: " + this.ID);
        System.out.println("The name is: " + this.name);
        return null;
    }
    /**
     * Faculty类中的toString()函数重构,
     * 利用getSimpleName()函数返回类名
     * 输出类名,ID和名字
     * 共三行
     * 返回一个空值:满足String类型的返回值要求
     */
}

class Stuff extends Employee        //Stuff类,继承自Employee类
{
    String rank , time;
    //采用String类型定义职务和入职日期信息

    Stuff(){};      //默认构造函数
    Stuff(int i, int p, String add, String e, String na , String rk , String tm) {
        super(i, p, add, e, na);
        this.rank = rk;
        this.time = tm;
    }
    /**
     * Stuff类的含参构造参数
     * 继承自父类Employee的构造函数
     * 参数分别为int类型的ID号、int类型的电话号码
     * String类型的地址,电子邮箱地址和名字
     * 还有Stuff类特有的String类型的职务和入职日期信息
     * 对这些变量进行初始化
     */
    public String toString()
    {
        System.out.println("Class name: " + Stuff.class.getSimpleName() + "(Extends from Person)");
        System.out.println(this.name + "'s ID is: " + this.ID);
        System.out.println("The name is: " + this.name);
        return null;
    }
    /**
     * Stuff类中的toString()函数重构,
     * 利用getSimpleName()函数返回类名
     * 输出类名,ID和名字
     * 共三行
     * 返回一个空值:满足String类型的返回值要求
     */
}
public class Main
{
    public static void main(String[] args)
    {
        Stuff []a = new Stuff[3];
        a[0] = new Stuff(123,63831110,"hfut","zs@163.com","zhangsan","Manger","2020-1");
        a[1] = new Stuff(456,63831111,"hfut","ls@163.com","lisi","cleaner","2010-2");
        a[2] = new Stuff(789,63831112,"hfut","ww@163.com","wangwu","CoManger","2019-8");
        /**
         *      初始化三个Stuff对象:a[0],a[1].a[2]
         *      调用Stuff类中的含参初始化函数
         */
        a[0].toString();
        a[1].toString();
        a[2].toString();

        Student []b = new Student[3];
        b[0] = new Student(1,63831210,"双子楼","zs@163.com","zhangsan","大一");
        b[1] = new Student(2,63831211,"双子楼","ls@163.com","lisi","大二");
        b[2] = new Student(3,63831212,"双子楼","ww@163.com","wangwu","大三");
        /**
         *      初始化三个Student对象:b[0],b[1].b[2]
         *      调用Student类中的含参初始化函数
         */
        b[0].toString();
        b[1].toString();
        b[2].toString();

        Faculty []c = new Faculty[2];
        c[0] = new Faculty(111,63831310,"教研室","zs@163.com","zhangsan","复变函数与积分变换","周二第一节","教授");
        c[1] = new Faculty(222,63831311,"教研室","ls@163.com","lisi","程序设计基础","周三上午","副教授");
        /**
         *      初始化两个Faculty对象:c[0],c[1]
         *      调用Faculty类中的含参初始化函数
         */
        c[0].toString();
        c[1].toString();

    }
}

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值