5.8
python学习
• 所花时间:2
• 代码行数:487
• 博客容量:1
• 代码如下:
# 定义教师接口
class Teacher:
def calculate_salary(self):
pass
# 教授类
class Professor(Teacher):
def __init__(self, name, gender, birthdate, teaching_hours):
self.name = name
self.gender = gender
self.birthdate = birthdate
self.teaching_hours = teaching_hours
def calculate_salary(self):
fixed_salary = 5000
hourly_allowance = 50
return fixed_salary + self.teaching_hours * hourly_allowance
# 副教授类
class AssociateProfessor(Teacher):
def __init__(self, name, gender, birthdate, teaching_hours):
self.name = name
self.gender = gender
self.birthdate = birthdate
self.teaching_hours = teaching_hours
def calculate_salary(self):
fixed_salary = 3000
hourly_allowance = 30
return fixed_salary + self.teaching_hours * hourly_allowance
# 讲师类
class Lecturer(Teacher):
def __init__(self, name, gender, birthdate, teaching_hours):
self.name = name
self.gender = gender
self.birthdate = birthdate
self.teaching_hours = teaching_hours
def calculate_salary(self):
fixed_salary = 2000
hourly_allowance = 20
return fixed_salary + self.teaching_hours * hourly_allowance
def main():
# 创建教师对象并计算月工资
professor = Professor("张教授", "男", "1975-03-15", 50)
print("{}的月工资为:{}元".format(professor.name, professor.calculate_salary()))
associate_professor = AssociateProfessor("王副教授", "女", "1980-07-20", 40)
print("{}的月工资为:{}元".format(associate_professor.name, associate_professor.calculate_salary()))
lecturer = Lecturer("李讲师", "男", "1990-01-10", 30)
print("{}的月工资为:{}元".format(lecturer.name, lecturer.calculate_salary()))
if __name__ == "__main__":
main()
浙公网安备 33010602011771号