3.14 以子类取代类型码
【1】原代码
1 class Employee 2 { 3 public: 4 const static int ENGINEER = 0; 5 const static int SALESMAN = 1; 6 const static int MANAGER = 2; 7 8 public: 9 Employee(); 10 11 int payAmount() 12 { // 典型的switch语句案例。 13 // 这里仅仅列出了一个函数的业务逻辑实现过程,实际中可能会有很多这样的函数,需要进行这样类似的条件判断 14 switch (m_nType) 15 { 16 case ENGINEER: 17 return m_monthlySalary; 18 case SALESMAN: 19 return m_monthlySalary + m_bonus; 20 case MANAGER: 21 return m_monthlySalary + m_commission; 22 default: 23 break; 24 } 25 } 26 27 private: 28 int m_nType; 29 int m_bonus; 30 int m_commission; 31 int m_monthlySalary; 32 };
【2】以子类取代类型码
Employee.h
1 #ifndef _EMPLOYEE_H 2 #define _EMPLOYEE_H 3 4 #include "EmployeeType.h" 5 class Employee 6 { 7 public: 8 Employee(int nType); 9 10 int getType(); 11 void setType(int nType); 12 int payAmount(); 13 14 private: 15 EmployeeType *m_pType; 16 }; 17 #endif
Employee.cpp
1 #include "Employee.h" 2 3 Employee::Employee(int nType) 4 { 5 setType(nType); 6 } 7 8 int Employee::getType() 9 { 10 return m_pType->getTypeCode(); 11 } 12 13 void Employee::setType(int nType) 14 { 15 m_pType = EmployeeType::newType(nType); 16 } 17 18 int Employee::payAmount() 19 { 20 return m_pType->payAmount(); 21 }
EmployeeType.h
1 #ifndef _EMPLOYEETYPE_H 2 #define _EMPLOYEETYPE_H 3 4 class EmployeeType 5 { 6 public: 7 const static int ENGINEER = 0; 8 const static int SALESMAN = 1; 9 const static int MANAGER = 2; 10 11 public: 12 EmployeeType(); 13 static EmployeeType* newType(int code); 14 15 virtual int payAmount() = 0; 16 virtual int getTypeCode() = 0; 17 18 public: 19 int m_bonus; 20 int m_commission; 21 int m_monthlySalary; 22 }; 23 24 #endif
EmpolyeeeType.cpp
1 #include "EmployeeType.h" 2 #include "Engineer.h" 3 #include "Manager.h" 4 #include "Salesman.h" 5 6 EmployeeType::EmployeeType() 7 : m_bonus(1000) 8 , m_commission(500) 9 , m_monthlySalary(2000) 10 {} 11 12 EmployeeType* EmployeeType::newType(int code) 13 { 14 switch (code) 15 { 16 case ENGINEER: 17 return (new Engineer()); 18 case SALESMAN: 19 return (new Salesman()); 20 case MANAGER: 21 return (new Manager()); 22 default: 23 return (new Engineer()); 24 } 25 }
Engineer.h
1 #ifndef _ENGINEER_H 2 #define _ENGINEER_H 3 4 #include "EmployeeType.h" 5 class Engineer : public EmployeeType 6 { 7 public: 8 Engineer(); 9 int getTypeCode(); 10 int payAmount(); 11 }; 12 13 #endif
Engineer.cpp
1 include "Engineer.h" 2 3 Engineer::Engineer() : EmployeeType() 4 {} 5 6 int Engineer::getTypeCode() 7 { 8 return ENGINEER; 9 } 10 11 int Engineer::payAmount() 12 { 13 return m_monthlySalary; 14 }
Manager.h
1 #ifndef _MANAGER_H 2 #define _MANAGER_H 3 4 #include "EmployeeType.h" 5 6 class Manager : public EmployeeType 7 { 8 public: 9 Manager(); 10 int getTypeCode(); 11 int payAmount(); 12 }; 13 14 #endif
Manager.cpp
1 #include "Manager.h" 2 3 Manager::Manager() : EmployeeType() 4 {} 5 6 int Manager::getTypeCode() 7 { 8 return MANAGER; 9 } 10 11 int Manager::payAmount() 12 { 13 return m_monthlySalary + m_commission; 14 }
Salesman.h
1 #ifndef _SALESAMAN_H 2 #define _SALESAMAN_H 3 4 #include "EmployeeType.h" 5 6 class Salesman : public EmployeeType 7 { 8 public: 9 Salesman(); 10 int getTypeCode(); 11 int payAmount(); 12 }; 13 14 #endif
Salesman.cpp
1 #include "Salesman.h" 2 3 Salesman::Salesman() : EmployeeType() 4 {} 5 6 int Salesman::getTypeCode() 7 { 8 return SALESMAN; 9 } 10 11 int Salesman::payAmount() 12 { 13 return m_monthlySalary + m_bonus; 14 }
main.cpp
1 #include "Employee.h" 2 3 void main() 4 { 5 Employee *pEngineer = new Employee(EmployeeType::ENGINEER); 6 pEngineer->payAmount(); 7 }
【3】总结
有一个不可变的类型码,它会影响类的行为。以子类取代这个类型码。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异