如何递归访问不同的类?

发布于 2024-10-20 05:24:10 字数 1430 浏览 2 评论 0原文

我想要一组可互换的类来对数据集执行操作。我“认为”我想做的就是所谓的多态性,但我不确定。

这是我正在尝试做的一个例子。我想创建一个包含初始值的对象,然后初始化另一个类以使用第一个类中的数据并使用 exec 执行操作,然后使用另一个递归类重复此操作。

我希望能够改变操作的顺序,这个想法是任何类都可以调用 exec() ,它总是返回 unsigned long 。 init() 可能不同,但在初始化期间调用,并且不会在递归类中访问。

谢谢,

class operationsObject {
public:
    virtual unsigned long exec (void) =0;
};

class addObject: public operationsObject {
private:
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b)
    {valueA = a; valueB = b;}
    unsigned long exec()
    {return valueA + valueB;}
};

class subtractObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA;
public:
    void init(unsigned long a, operationsObject *o)
    {valueA = a; obj = o;}
    unsigned long exec()
    {return obj->exec() - valueA;}
};

class multiplyObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b, operationsObject *o)
    {valueA = a; valueB = b; obj = o;}
    unsigned long exec()
    {return obj->exec() * valueA * valueB;}
};

int main(){
    operationsObject *op1 = new addObject;
    operationsObject *op2 = new subtractObject;
    operationsObject *op3 = new multiplyObject;
    op1->init(4,5);
    op2->init(4, op1);
    op3->init(1, 2, op2);
    unsigned retVal = op3->exec();
}

I want to have a set of interchangeable classes to perform operations on a dataset. I "think" what I'm trying to do is called Polymorphism but I'm unsure.

This is an example of what I'm trying to do. I want to create an object which contains the initial values, then initialize another class to use the data in the first class and perform a operation using exec, then repeat this with another recursive class.

I want to be able to change the order of operations, the idea is any class can call exec() which will always return unsigned long. init() might be different but is called during initialization and won't be accessed within a recursive class.

Thanks,

class operationsObject {
public:
    virtual unsigned long exec (void) =0;
};

class addObject: public operationsObject {
private:
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b)
    {valueA = a; valueB = b;}
    unsigned long exec()
    {return valueA + valueB;}
};

class subtractObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA;
public:
    void init(unsigned long a, operationsObject *o)
    {valueA = a; obj = o;}
    unsigned long exec()
    {return obj->exec() - valueA;}
};

class multiplyObject: public operationsObject {
private:
    operationsObject *obj;
    unsigned long valueA, valueB;
public:
    void init(unsigned long a, unsigned long b, operationsObject *o)
    {valueA = a; valueB = b; obj = o;}
    unsigned long exec()
    {return obj->exec() * valueA * valueB;}
};

int main(){
    operationsObject *op1 = new addObject;
    operationsObject *op2 = new subtractObject;
    operationsObject *op3 = new multiplyObject;
    op1->init(4,5);
    op2->init(4, op1);
    op3->init(1, 2, op2);
    unsigned retVal = op3->exec();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-10-27 05:24:10

您的描述听起来像是您可能想要检查以下设计模式:责任链

我可能会想出这样的东西:

  • 命令接口/抽象基类,带有
  • 公共方法 exec (非虚拟!),调用
  • 受保护的抽象虚拟方法 execLocal实现您正在寻找的行为。

现在,exec 可以定义为:

def exec(param):
    self.execLocal(param)
    if self.hasNext():
        self.next.exec(param)

Your description sounds like you might want to check the following design pattern: Chain of Responsibility.

I'd probably come up with something like this:

  • command interface / abstract base class with
  • public method exec (non-virtual!) that calls a
  • protected abstract virtual method execLocal that implements the behavior you are looking for.

Now, exec could be defined as:

def exec(param):
    self.execLocal(param)
    if self.hasNext():
        self.next.exec(param)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文