2024/5/15
进行python实验:设计教师接口,该接口包含教师工资计算方法。应用(一)中的高校人员信息包,设计不同职称的教师类:教授,副教授,讲师,教师的基本信息包括姓名、性别、出生年月、职称、课时工作量等属性。注意学校对教师每月工资的计算规定如下:固定工资+课时补贴;教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。
from abc import ABC, abstractmethod class Teacher(ABC): def __init__(self, name, gender, birthdate, title, teaching_hours): self.name = name self.gender = gender self.birthdate = birthdate self.title = title self.teaching_hours = teaching_hours @abstractmethod def calculate_salary(self): pass def display(self): print(f"姓名: {self.name}") print(f"性别: {self.gender}") print(f"出生年月: {self.birthdate}") print(f"职称: {self.title}") print(f"工作课时: {self.teaching_hours}") class Professor(Teacher): def __init__(self, name, gender, birthdate, teaching_hours): super().__init__(name, gender, birthdate, "教授", teaching_hours) def calculate_salary(self): fixed_salary = 5000 hourly_allowance = 50 total_salary = fixed_salary + self.teaching_hours * hourly_allowance return total_salary class AssociateProfessor(Teacher): def __init__(self, name, gender, birthdate, teaching_hours): super().__init__(name, gender, birthdate, "副教授", teaching_hours) def calculate_salary(self): fixed_salary = 3000 hourly_allowance = 30 total_salary = fixed_salary + self.teaching_hours * hourly_allowance return total_salary class Lecturer(Teacher): def __init__(self, name, gender, birthdate, teaching_hours): super().__init__(name, gender, birthdate, "讲师", teaching_hours) def calculate_salary(self): fixed_salary = 2000 hourly_allowance = 20 total_salary = fixed_salary + self.teaching_hours * hourly_allowance return total_salary # Create instances of different teacher classes professor1 = Professor("张三", "男", "1980-05-20", 80) associate_professor1 = AssociateProfessor("李四", "女", "1985-10-15", 60) lecturer1 = Lecturer("王五", "男", "1990-03-10", 40) # Display information and salary of each teacher professor1.display() print(f"月工资: {professor1.calculate_salary()}元\n") associate_professor1.display() print(f"月工资: {associate_professor1.calculate_salary()}元\n") lecturer1.display() print(f"月工资: {lecturer1.calculate_salary()}元\n")
浙公网安备 33010602011771号