本文介绍了怎么设计这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个关于类层次结构设计的新手问题。 假设我有一个Class0类,需要实现公共 Class0.compute()接口。选择 有三种不同的方法,而且该类的用户必须能够选择这个选项。这就是我的想法。我声明了三个类, ClassA,ClassB,ClassC,并且每个类都有一个可以由Class0使用的独特的compute() 方法。 Class0是一个模板类, 如下: 模板< class T> class Class0 { public: double compute(){return p-> compute(); } 私人: T * p; }; 然后我可以这样做: Class0< ClassA> C; C.compute(); 问题:classA.compute(),classB.compute()和classC.compute()all 具有不同数量和类型的参数。我可能还想在将来添加classD等。这意味着我需要在Class0的公共区域中实现 三个不同的compute()函数。 或者,我可以在Class0中编写一个方法从指针p返回一个 对应的compute()函数,但这并不是看起来很优雅。 另一个我的想法是宣布Class0是Class {A,B,C}的后代: template< class T> class Class0:public T { .... } 然后 Class0< classA> C; 然后Class0将从类ClassA等继承compute()。 让我感到不舒服的是Class0和类Class {A,B, C} 实际上没有is-a关系。 A,B,C只提供了进行某些计算的手段 。 任何更好的方法来完成我想做的事情? 非常感谢, - 弗拉基米尔I have a rather newbie question regarding design of class hierarchy.Suppose I have a class Class0, and need to implement a publicClass0.compute() interface. There are three different ways to choosethe implementation, and the user of the class has to be able to makethat choice. Here is what I have in mind. I declare three classes,ClassA, ClassB, ClassC, and each of them has a distinct compute()method that can be used by Class0. Class0 is a template class, asfollows:template <class T>class Class0 {public:double compute() { return p->compute(); }private:T *p;};Then I could do:Class0<ClassA> C;C.compute();Problem: classA.compute(), classB.compute() and classC.compute() allhave different number and types of parameters. I might also want toadd classD, etc. in the future. This means I would need to implementthree different compute() functions in the public area of Class0.Alternatively, I could write a method in Class0 that returns acorresponding compute() function from the pointer p, but this doesn''tseem elegant.Another idea I have is to declare Class0 a descendant of Class{A,B,C}:template<class T>class Class0 : public T {....}and thenClass0<classA> C;Then Class0 will inherit compute() from classes ClassA, etc.What makes me uncomfortable is that Class0 and classes Class{A,B,C}do not really have "is-a" relationship. A,B,C only provide the meansto do certain computations.Any better way to accomplish what I am trying to do?Many thanks,--Vladimir推荐答案 如果这三个版本的参数数量和类型不同,那么我就是b $ b don'看不出任何理由要有不同的课程。为什么不只有 三个不同的compute()函数?类似的东西: public: double compute(); double compute(double param1); double compute(double param1,int param2); 这样的东西会起作用吗? -HowardIf the three versions have different number and types of parameters, then Idon''t see any reason to have distinct classes at all. Why not just havethree different compute() functions? Something like:public:double compute( );double compute( double param1 );double compute( double param1, int param2 );Will something like this work?-Howard 我可能会投票给第三种方式。 VI''d probably vote for the third way.V 可能会有更多。There could be more. 吧。right. _A_方法?我认为你实际上需要编写尽可能多的方法,因为会有用于实例化Class0的类。你可以通过引入一个成员_template_ 模板< class T>隐式地这样做。 class Class0 { ... 模板<类U> U get_compute(){return& T :: compute;不知怎的(不确定这会起作用,怎么会推断出来?) _A_ method? I think you actually will need to write as many methods as there will be classes used to instantiate Class0. You can do it implicitly by introducing a member _template_ template<class T> class Class0 { ... template<class U> U get_compute() { return &T::compute; } somehow (not sure this is going to work, though, how would U be deduced?) 嗯?首先,你不应该把''Class0''称为_class_因为它不是一个类。这是一个模板。第二,Class0< ClassA> _class_确实有is-a和is-a。与ClassA的关系因为它是公开继承的。 Class0< ClassB>也是如此。使用ClassB,Class0< ClassC>分别使用 ClassC。 Huh? First of all, you shouldn''t call ''Class0'' a _class_ because it is not a class. It''s a template. Second, Class0<ClassA> _class_ does have the "is-a" relationship with ClassA because it is publicly inherited from it. And so does Class0<ClassB> with ClassB, and Class0<ClassC> with ClassC, respectively. 原谅我的语言。 Class0< ClassA的>当然,这与声明的编写方式有is-a 关系。我的意思 说是_logically_我感觉不舒服 Class0< ClassA>来自ClassA,因为ClassA仅为Class0< ClassA>提供了compute()函数的 实现。 - 弗拉基米尔Forgive my language. Class0<ClassA> does of course have the "is-a"relationship the way the declarations are written. What I meant tosay was that _logically_ I don''t feel comfortable derivingClass0<ClassA> from ClassA, since ClassA only provides theimplementation of a compute() function for Class0<ClassA>.--Vladimir 这篇关于怎么设计这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-14 12:29