点击查看代码
#类继承练习:人力系统
# 员工分为两类,全职员工FullTimeEmployee、兼职员工PartTimeEmployee
# 全职和兼职都有”姓名,name,工号:id属性
# 都具备 打印信息 print_info(打印姓名、工号)方法
# 全职有月薪 monthly_salary属性
# 兼职有日薪 daily_salary属性,每月工作天数 work_days的属性
# 全薪和兼职都有 计算月薪calculate_monthly_pay的方法,但具体计算过程不一样
class Employee:
def __init__(self,name,id):
self.name = name
self.id = id
def print_info(self):
print(f"员工名字:{self.name},工号:{self.id}")
class FullTimeEmployee(Employee):
def __init__(self,name,id,monthly_salary):
super().__init__(name,id)
self.monthly_salary = monthly_salary
def calculate_monthly_pay(self):
return self.monthly_salary
class PartTimeEmployee(Employee):
def __init__(self,name,id,daily_sasary,work_days):
super().__init__(name,id)
self.daily_salary = daily_sasary
self.work_days = work_days
def calculate_monthly_pay(self):
return self.daily_salary * self.work_days
zhangsan = FullTimeEmployee("张三","1001",600)
lisi = PartTimeEmployee("李四","1002",230,16)
zhangsan.print_info()
lisi.print_info()
print(zhangsan.calculate_monthly_pay())
print(lisi.calculate_monthly_pay())
输出: