Python-100-Days/Day01-15/code/Day09/employee.py

76 lines
1.8 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
抽象类 / 方法重写 / 多态
实现一个工资结算系统 公司有三种类型的员工
2019-05-03 21:17:36 +08:00
- 部门经理固定月薪12000元/
- 程序员按本月工作小时数每小时100元
- 销售员1500元/月的底薪加上本月销售额5%的提成
2018-04-27 00:00:22 +08:00
输入员工的信息 输出每位员工的月薪信息
Version: 0.1
Author: 骆昊
Date: 2018-03-12
"""
from abc import ABCMeta, abstractmethod
class Employee(object, metaclass=ABCMeta):
2019-05-03 21:17:36 +08:00
def __init__(self, name):
self._name = name
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def name(self):
return self._name
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@abstractmethod
def get_salary(self):
pass
2018-04-27 00:00:22 +08:00
class Manager(Employee):
2019-05-03 21:17:36 +08:00
# 想一想: 如果不定义构造方法会怎么样
def __init__(self, name):
# 想一想: 如果不调用父类构造器会怎么样
super().__init__(name)
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def get_salary(self):
return 12000
2018-04-27 00:00:22 +08:00
class Programmer(Employee):
2019-05-03 21:17:36 +08:00
def __init__(self, name):
super().__init__(name)
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def set_working_hour(self, working_hour):
self._working_hour = working_hour
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def get_salary(self):
return 100 * self._working_hour
2018-04-27 00:00:22 +08:00
class Salesman(Employee):
2019-05-03 21:17:36 +08:00
def __init__(self, name):
super().__init__(name)
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def set_sales(self, sales):
self._sales = sales
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def get_salary(self):
return 1500 + self._sales * 0.05
2018-04-27 00:00:22 +08:00
if __name__ == '__main__':
2019-05-03 21:17:36 +08:00
emps = [Manager('武则天'), Programmer('狄仁杰'), Salesman('白元芳')]
for emp in emps:
if isinstance(emp, Programmer):
working_hour = int(input('请输入%s本月工作时间: ' % emp.name))
emp.set_working_hour(working_hour)
elif isinstance(emp, Salesman):
sales = float(input('请输入%s本月销售额: ' % emp.name))
emp.set_sales(sales)
print('%s本月月薪为: ¥%.2f' % (emp.name, emp.get_salary()))