2024/4/12

The-rich發表於2024-06-19

進行python程式設計:

定義一個人員類People,其屬性有:姓名、性別、年齡;基於People實現學生類Student,新增屬性:學號、入學時間和入學成績;基於People實現教師類Teacher,新增屬性:職務、部門、工作時間;基於Student實現究生類Graduate,新增屬性:研究方向和導師,分別定義其中的建構函式和輸出函式。程式中定義各種類的物件,並完成測試。 基於以上類利用介面實現在職研究生類 GradOnWork。

class People:
    def __init__(self, name, gender, age):
        self.name = name
        self.gender = gender
        self.age = age

    def display_info(self):
        print(f"姓名: {self.name}, 性別: {self.gender}, 年齡: {self.age}")


class Student(People):
    def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score):
        super().__init__(name, gender, age)
        self.student_id = student_id
        self.enrollment_date = enrollment_date
        self.entrance_score = entrance_score

    def display_info(self):
        super().display_info()
        print(f"學號: {self.student_id}, 入學時間: {self.enrollment_date}, 入學成績: {self.entrance_score}")


class Teacher(People):
    def __init__(self, name, gender, age, position, department, work_years):
        super().__init__(name, gender, age)
        self.position = position
        self.department = department
        self.work_years = work_years

    def display_info(self):
        super().display_info()
        print(f"職務: {self.position}, 部門: {self.department},工作時間: {self.work_years}")


class Graduate(Student):
    def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score, research_area, advisor):
        super().__init__(name, gender, age, student_id, enrollment_date, entrance_score)
        self.research_area = research_area
        self.advisor = advisor

    def display_info(self):
        super().display_info()
        print(f"研究方向: {self.research_area}, 導師: {self.advisor}")


class GradOnWork(Graduate):
    def __init__(self, name, gender, age, student_id, enrollment_date, entrance_score, research_area, advisor, company):
        super().__init__(name, gender, age, student_id, enrollment_date, entrance_score, research_area, advisor)
        self.company = company

def display_info(self):
    super().display_info()
    print(f"Company: {self.company}")


# Testing the classes
student1 = Student("張三", "", 20, "S12345", "2022-09-01", 95)
teacher1 = Teacher("李四", "", 35, "教授", "軟體工程", 10)
graduate1 = Graduate("王五", "", 25, "G54321", "2020-09-01", 90, "計算機", "李田所")
grad_on_work1 = GradOnWork("田所浩二", "", 27, "114514", "1919-08-10", 88, "下北澤飲食研究", "李田所", "Tech Company")

student1.display_info()
teacher1.display_info()
graduate1.display_info()
grad_on_work1.display_info()

相關文章