Refactor: replace type code with class (c++)

来源:互联网 发布:数据结构英文版 c语言 编辑:程序博客网 时间:2024/05/16 05:36

重构前

class Person {public:static const int A = 0;static const int B = 1;static const int AB = 2;static const int O = 3;Person(int bloodType) {this->bloodType = bloodType;}int getBloodType() {return this->bloodType;}void setBloodType(int bloodType) {this->bloodType = bloodType;}private:int bloodType;};Person zhang(0);Person wang(1);

重构后

class BloodType {public:static BloodType A;static BloodType B;static BloodType AB;static BloodType O;explicit BloodType(int code) {this->_code = code;}private:int _code;};BloodType BloodType::A = BloodType(0);BloodType BloodType::B = BloodType(1);BloodType BloodType::AB = BloodType(2);BloodType BloodType::O = BloodType(3);class PersonRF {public:PersonRF(BloodType btype):bloodType(btype){}BloodType getBloodType() {return this->bloodType;}void setBloodType(BloodType bloodType) {this->bloodType = bloodType;}private:BloodType bloodType;};PersonRF li(BloodType::A);PersonRF zhao(BloodType::B);


0 0
原创粉丝点击