有人可以解释多态性的好处吗? [英] Can someone explain the benefits of polymorphism?

查看:154
本文介绍了有人可以解释多态性的好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很了解它是如何工作的,但我只是不能理解什么使它有用。你仍然需要定义所有单独的函数,你仍然必须创建每个对象的实例,所以为什么不从该对象调用函数vs创建对象,创建一个指向父对象的指针并传递派生对象引用,只是调用一个函数?我不明白采取这个额外步骤的好处。



为什么这样:

  class Parent 
{
virtual void function(){};
};

class Derived:public Parent
{
void function()
{
cout< derived;
}
};

int main()
{
派生foo;
Parent * bar =& foo;
bar-> function();
return -3234324;
}

vs:

  class Parent 
{
virtual void function(){};
};

class Derived:public Parent
{
void function()
{
cout< derived;
}
};

int main()
{
派生foo;
foo.function();
return -3234324;
}

他们做的完全一样吗?

解决方案

你的例子都做同样的事情,但在<

第一个示例使用 的着名 SOLID设计原则 strong>。


So I understand pretty much how it works, but I just can't grasp what makes it useful. You still have to define all the separate functions, you still have to create an instance of each object, so why not just call the function from that object vs creating the object, creating a pointer to the parent object and passing the derived objects reference, just to call a function? I don't understand the benefits of taking this extra step.

Why do this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    Parent* bar = &foo;
    bar->function();
    return -3234324;
}

vs this:

class Parent
{
    virtual void function(){};
};

class Derived : public Parent
{
    void function()
    {
    cout << "derived";
    }
};

int main()
{
    Derived foo;
    foo.function();
    return -3234324;
}

They do exactly the same thing right? Only one uses more memory and more confusion as far as I can tell.

解决方案

Both your examples do the same thing but in different ways.
The first example calls function() by using Static binding while the second calls it using Dynamic Binding.

In first case the compiler precisely knows which function to call at compilation time itself, while in second case the decision as to which function should be called is made at run-time depending on the type of object which is pointed by the Base class pointer.

What is the advantage?
The advantage is more generic and loosely coupled code.

Imagine a class hierarchy as follows:

The calling code which uses these classes, will be like:

Shape *basep[] = { &line_obj, &tri_obj,
                   &rect_obj, &cir_obj};
for (i = 0; i < NO_PICTURES; i++)
    basep[i] -> Draw ();

Where, line_obj, tri_obj etc are objects of the concrete Shape classes Line, Triangle and so on, and they are stored in a array of pointers of the type of more generalized base class Shape.

This gives the additional flexibility and loose coupling that if you need to add another concrete shape class say Rhombus, the calling code does not have to change much, because it refers to all concrete shapes with a pointer to Base class Shape. You only have to make the Base class pointer point to the new concrete class.

At the sametime the calling code can call appropriate methods of those classes because the Draw() method would be virtual in these classes and the method to call will be decided at run-time depending on what object the base class pointer points to.

The above is an good example of applying Open Closed Principle of the famous SOLID design principles.

这篇关于有人可以解释多态性的好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆