【1】源代码
1 class Person 2 { 3 public: 4 QString getTelephoneNumber() 5 { 6 return ("(" + m_officeAreaCode + ")" + m_officeNumber); 7 } 8 9 QString get_name()10 {11 return m_name;12 }13 void set_name(QString strName)14 {15 m_name = strName;16 }17 18 int get_age()19 {20 return m_nAge;21 }22 void set_age(int nAge)23 {24 m_nAge = nAge;25 }26 27 bool get_male()28 {29 return m_bMale;30 }31 void set_male(bool bMale)32 {33 m_bMale = bMale;34 }35 36 QString get_address()37 {38 return m_address;39 }40 void set_address(QString strAddress)41 {42 m_address = strAddress;43 }44 45 QString get_officeAreaCode()46 {47 return m_officeAreaCode;48 }49 50 void set_officeAreaCode(QString areaCode)51 {52 m_officeAreaCode = areaCode;53 }54 55 QString get_officeNumber()56 {57 return m_officeNumber;58 }59 60 void set_officeNumber(QString number)61 {62 m_officeNumber = number;63 }64 private:65 int m_nAge; // 年龄66 bool m_bMale; // 性别67 QString m_name; // 姓名68 QString m_address; // 地址69 QString m_officeAreaCode; // 区号70 QString m_officeNumber; // 电话号码71 };
【2】提炼类
1 class TelephoneNumber 2 { 3 public: 4 QString getTelephoneNumber() 5 { 6 return ("(" + m_areaCode + ")" + m_officeNumber); 7 } 8 9 QString get_AreaCode()10 {11 return m_areaCode;12 }13 14 void set_AreaCode(QString areaCode)15 {16 m_areaCode = areaCode;17 }18 19 QString get_officeNumber()20 {21 return m_officeNumber;22 }23 24 void set_officeNumber(QString number)25 {26 m_officeNumber = number;27 }28 29 private:30 QString m_areaCode; // 区号31 QString m_officeNumber; // 电话号码32 };33 34 class Person35 {36 public:37 QString get_name()38 {39 return m_name;40 }41 void set_name(QString strName)42 {43 m_name = strName;44 }45 46 int get_age()47 {48 return m_nAge;49 }50 void set_age(int nAge)51 {52 m_nAge = nAge;53 }54 55 bool get_male()56 {57 return m_bMale;58 }59 void set_male(bool bMale)60 {61 m_bMale = bMale;62 }63 64 QString get_address()65 {66 return m_address;67 }68 void set_address(QString strAddress)69 {70 m_address = strAddress;71 }72 73 QString getTelephoneNumber()74 {75 return (m_pTelNumber != NULL) ? m_pTelNumber->getTelephoneNumber() : QString();76 }77 78 TelephoneNumber* getTelephone()79 {80 return m_pTelNumber;81 }82 83 private:84 TelephoneNumber* m_pTelNumber;85 86 int m_nAge;87 bool m_bMale;88 QString m_name;89 QString m_address;90 };
【3】总结
某个类做了应该由两个类做的事。建立一个新类,将相关的字段和函数从旧类搬移到新类。
Good Good Study, Day Day Up.
顺序 选择 循环 总结