欢迎进行能更好地概括问题的标题编辑。
我想以某种方式重构这三个类以删除其中表示的重复字段class C
(请参阅层次结构)。我想过将字段拉到父类中,但问题是 A 和 B 不够相似,不能被认为是“is-a”,C 被认为是两者,它实际上只是一个成员字段,所以创建一个类仅仅持有一件事似乎有点矫枉过正。
等级制度:
(abstract data type)
class A : public O {
public:
//...
std::string GetName();
std::string GetName() const;
void SetName(std::string name);
//...
protected:
//...
std::string _name;
//...
};
//Methods and fields shown here represent the exact same representative data as in A but the classes are so dissimilar as to not be considered "is-a" relationship.
(abstract data type)
class B {
public:
//...
std::string GetName();
std::string GetName() const;
void SetName(std::string name);
//...
protected:
//...
std::string _name;
//...
};
(concrete)
class C : public A, public B {
public:
//...
C(/*..Other parameters..*/, std::string name, /*....*/)
: A(name, /*...*/), B(name, /*...*/) {
/*...*/
}
//...
private:
//...
};