C接口,继承,多态
想象一下这种情况:
> interface I1:m1(),由C1类实现 我想定义带参数arg的函数. void f1(I1 arg) { use m1() } void f2([I1,I2] arg) { use m1() and m2() } void f3([I2,I3] arg) { use m2() and m3() } 然后我想定义: >一个“联合”接口I123,它具有所有方法,由C123实现 然后我想实例化C123并将其与f1,f2,f3一起使用. C123 obj; f1(obj); f2(obj); f3(obj); 问题 >这可能在C? 代码 #include <string> #include <iostream> using namespace std; class C1 { protected: int i; public: int getI() const { return i; } void setI(int i_) { i = i_; } }; class C2 { protected: string s; public: string getS() const { return s; } void setS(string s_) { s = s_; } }; class C3 { protected: float f; public: float getF() const { return f; } void setF(float f_) { f = f_; } }; class C23 : public C2,public C3 {}; class C123 : public C1,public C2,public C3 {}; void f3(C23 arg) { arg.setS(to_string(arg.getF())); } int main() { C123 obj; f3(obj); std::cout << obj.getS(); } 错误消息 a.cc: In function ‘int main()’: a.cc:42:9: error: could not convert ‘obj’ from ‘C123’ to ‘C23’ f3(obj); 解决方法
是的,可以在C中做到这一点. Example solution using interfaces #include <string> #include <iostream> using namespace std; class I1 { public: virtual int getI() const = 0; virtual void setI(int i) = 0; }; class I2 { public: virtual string getS() const = 0; virtual void setS(string s) = 0; }; class I3 { public: virtual float getF() const = 0; virtual void setF(float f) = 0; }; class C1 : public I1 { protected: int i; public: int getI() const { return i; } void setI(int i_) { i = i_; } }; class C12 : public I1,public I2 { protected: int i; string s; public: int getI() const { return i; } void setI(int i_) { i = i_; } string getS() const { return s; } void setS(string s_) { s = s_; } }; class C123 : public I1,public I2,public I3 { protected: int i; string s; float f; public: int getI() const { return i; } void setI(int i_) { i = i_; } string getS() const { return s; } void setS(string s_) { s = s_; } float getF() const { return f; } void setF(float f_) { f = f_; } }; template<class T> void f1(const T& c1) { cout << "f1:n"; cout << " getI: " << c1.getI() << endl; } template<class T> void f2(const T& c12) { cout << "f2:n"; cout << " getI: " << c12.getI() << endl; cout << " getS: " << c12.getS() << endl; } template<class T> void f3(const T& c23) { cout << "f3:n"; cout << " getS: " << c23.getS() << endl; cout << " getF: " << c23.getF() << endl; } void test() { C1 c1; c1.setI(1); f1(c1); cout << "n===== " << endl; C12 c12; c12.setI(12); c12.setS("str12"); f1(c12); f2(c12); cout << "n===== " << endl; C123 c123; c123.setI(123); c123.setF(1.23f); c123.setS("str123"); f1(c123); f2(c123); f3(c123); cout << "n===== " << endl; } int main() { test(); }
是的,没有接口就可以做到这一点. Solution using virtual inheritance (without using interfaces): #include <string> #include <iostream> using namespace std; class C1 { protected: int i; public: int getI() const { return i; } void setI(int i_) { i = i_; } }; class C2 { protected: string s; public: string getS() const { return s; } void setS(string s_) { s = s_; } }; class C3 { protected: float f; public: float getF() const { return f; } void setF(float f_) { f = f_; } }; class C12 : public virtual C1,public virtual C2 { }; class C23 : public virtual C2,public virtual C3 { }; class C123 : public virtual C1,public virtual C12,public virtual C23 { }; void f1(const C1& c1) { cout << "f1:n"; cout << " getI: " << c1.getI() << endl; } void f2(const C12& c12) { cout << "f2:n"; cout << " getI: " << c12.getI() << endl; cout << " getS: " << c12.getS() << endl; } void f3(const C23& c23) { cout << "f3:n"; cout << " getS: " << c23.getS() << endl; cout << " getF: " << c23.getF() << endl; } void test() { C1 c1; c1.setI(1); f1(c1); cout << "n===== " << endl; C12 c12; c12.setI(12); c12.setS("str12"); f1(c12); f2(c12); cout << "n===== " << endl; C123 c123; c123.setI(123); c123.setF(1.23f); c123.setS("str123"); f1(c123); f2(c123); f3(c123); cout << "n===== " << endl; } int main() { test(); } 一些解释.如果你需要你的f2只采用C1和C2(没有C3)那么它必须是在C1和C2上都继承的单独类型. f3也是如此.然后,如果您遵循该逻辑并在不使用虚拟继承的情况下创建类C12和C23,那么现在应该从C12和C23继承的C123将以C2的多个副本结束,当您调用f2和f3时,您将获得不同的值GETS.虚拟继承确保继承层次结构中只有一个类的副本.
Solution that use virtual inheritance and interfaces #include <string> #include <iostream> using namespace std; class I1 { public: virtual int getI() const = 0; virtual void setI(int i) = 0; }; class I2 { public: virtual string getS() const = 0; virtual void setS(string s) = 0; }; class I3 { public: virtual float getF() const = 0; virtual void setF(float f) = 0; }; class I12 : virtual public I1,virtual public I2 {}; class I23 : virtual public I2,virtual public I3 {}; class I123 : virtual public I12,virtual public I23 {}; class C1 : virtual public I1 { protected: int i; public: int getI() const { return i; } void setI(int i_) { i = i_; } }; class C2 : virtual public I2 { protected: string s; public: string getS() const { return s; } void setS(string s_) { s = s_; } }; class C3 : virtual public I3 { protected: float f; public: float getF() const { return f; } void setF(float f_) { f = f_; } }; class C12 : public I12,public C1,public C2 {}; class C123 : public I123,public C3 {}; void f1(const I1& c1) { cout << "f1:n"; cout << " getI: " << c1.getI() << endl; } void f2(const I12& c12) { cout << "f2:n"; cout << " getI: " << c12.getI() << endl; cout << " getS: " << c12.getS() << endl; } void f3(const I123& c23) { cout << "f3:n"; cout << " getS: " << c23.getS() << endl; cout << " getF: " << c23.getF() << endl; } void test() { C1 c1; c1.setI(1); f1(c1); cout << "n===== " << endl; C12 c12; c12.setI(12); c12.setS("str12"); f1(c12); f2(c12); cout << "n===== " << endl; C123 c123; c123.setI(123); c123.setF(1.23f); c123.setS("str123"); f1(c123); f2(c123); f3(c123); cout << "n===== " << endl; } int main() { test(); } 所有解决方案都应产生此输出: f1: getI: 1 ===== f1: getI: 12 f2: getI: 12 getS: str12 ===== f1: getI: 123 f2: getI: 123 getS: str123 f3: getS: str123 getF: 1.23 ===== (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |