python-设计高校人员信息包,并完成测试

题目描述定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork

【源代码程序】

class People:

    def __init__(self, name='Yuan', gender='女', age=20):

        self.name = name

        self.gender = gender

        self.age = age

 

    def display(self):

        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(self.age))

 

 

class Student(People):

    def __init__(self, name='Yuan', gender='女', age=20, student_id="20223794", into_time="2019-09-01",

                 into_score="100.0"):

        super().__init__(name, gender, age)

        self.student_id = student_id

        self.into_time = into_time

        self.into_score = into_score

 

    def display(self):

        super().display()

        print("学号:" + self.student_id + "\n入学时间:" + self.into_time + "\n入学成绩:" + str(self.into_score))

 

 

class Teacher(People):

    def __init__(self, name='Yuan', gender='女', age=20, post="院长", department="软件工程学院",

                 job_time="2008-12-03"):

        super().__init__(name, gender, age)

        self.post = post

        self.department = department

        self.job_time = job_time

 

    def display(self):

        super().display()

        print("职务:" + self.post + "\n部门:" + self.department + "\n工作时间:" + self.job_time)

 

 

class Graduate(Student):

    def __init__(self, name='Yuan', gender='女', age=20, student_id="20193250", into_time="2019-09-01",

                 into_score="100.0", research_fields="软件工程", teacher="Mr.Liu"):

        super().__init__(name, gender, age, student_id, into_time, into_score)

        self.research_fields = research_fields

        self.teacher = teacher

 

    def display(self):

        super().display()

        print("研究方向:" + self.research_fields + "\n导师:" + self.teacher)

 

 

class GradOnWork:

    def __init__(self, teacher, graduate):

        self.teacher = teacher

        self.graduate = graduate

 

    def display(self):

        self.teacher.display()

        print("----")

        self.graduate.display()

 

 

# Testing

if __name__ == "__main__":

    # Creating objects

    person1 = People("yuan", "女", 20)

    student1 = Student("1", "男", 22, "20210001", "2021-09-01", 95)

    teacher1 = Teacher("2", "男", 40, "教授", "软件工程", "2010-03-15")

    graduate1 = Graduate("3", "男", 28, "20180001", "2018-09-01", 88, "人工智能", "Dr. Liu")

 

    # Displaying information

    print("=== Person Information ===")

    person1.display()

    print("\n=== Student Information ===")

    student1.display()

    print("\n=== Teacher Information ===")

    teacher1.display()

    print("\n=== Graduate Information ===")

    graduate1.display()

 

    # Testing GradOnWork

    print("\n=== GradOnWork Information ===")

    grad_on_work = GradOnWork(teacher1, graduate1)

    grad_on_work.display()

 

运行测试

 

 

posted @ 2024-05-08 08:42  不会JAVA的小袁  阅读(21)  评论(0编辑  收藏  举报