CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day16-20/code/example12.py
Views: 729
1
"""
2
面向对象的三大支柱:封装、继承、多态
3
面向对象的设计原则:SOLID原则
4
面向对象的设计模式:GoF设计模式(单例、工厂、代理、策略、迭代器)
5
月薪结算系统 - 部门经理每月15000 程序员每小时200 销售员1800底薪加销售额5%提成
6
"""
7
from abc import ABCMeta, abstractmethod
8
9
10
class Employee(metaclass=ABCMeta):
11
"""员工(抽象类)"""
12
13
def __init__(self, name):
14
self.name = name
15
16
@abstractmethod
17
def get_salary(self):
18
"""结算月薪(抽象方法)"""
19
pass
20
21
22
class Manager(Employee):
23
"""部门经理"""
24
25
def get_salary(self):
26
return 15000.0
27
28
29
class Programmer(Employee):
30
"""程序员"""
31
32
def __init__(self, name, working_hour=0):
33
self.working_hour = working_hour
34
super().__init__(name)
35
36
def get_salary(self):
37
return 200.0 * self.working_hour
38
39
40
class Salesman(Employee):
41
"""销售员"""
42
43
def __init__(self, name, sales=0.0):
44
self.sales = sales
45
super().__init__(name)
46
47
def get_salary(self):
48
return 1800.0 + self.sales * 0.05
49
50
51
class EmployeeFactory():
52
"""创建员工的工厂(工厂模式 - 通过工厂实现对象使用者和对象之间的解耦合)"""
53
54
@staticmethod
55
def create(emp_type, *args, **kwargs):
56
"""创建员工"""
57
emp_type = emp_type.upper()
58
emp = None
59
if emp_type == 'M':
60
emp = Manager(*args, **kwargs)
61
elif emp_type == 'P':
62
emp = Programmer(*args, **kwargs)
63
elif emp_type == 'S':
64
emp = Salesman(*args, **kwargs)
65
return emp
66
67