重构前
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);