49、多态的概念和意义

解决函数重写碰到赋值兼容性原则的矛盾

父类中被重写的函数依然会继承给子类,子类中重写的函数将覆盖父类中的函数,通过作用域分辨符(::)可以访问到父类中的函数。

child c;

parent* p=&c;

c.parent::print();    // 从父类中继承

c.print();         // 在子类中重写

p->print();     //打印父类中定义->不是我们期望的,因为p指向子类对象,我们期望打印子类中的函数

面向对象中期望的行为:根据实际的对象类型判断如何调用重写函数。

父类指针(引用)指向-父类对象则调用父类中定义的函数,子类对象则调用子类中定义的重写函数。(根据实际对象类型而不是根据指针类型)

面向对象中的多态的概念:根据实际的对象类型决定函数调用的具体目标。同样的调用语句在实际运行时有多种不同的表现形态。

p->print();p指向父类对象,调用: 

void print()

{

    cout<<"i am parent"<<endl;

}

p指向子类对象,调用:

void print()

{

    cout<<"i am child"<<endl;

}

c++语言直接支持多态的概念:通过使用 virtual关键字对多态进行支持,被virtual声明的函数被重写后具有多态特性,被 virtual 声明的函数叫做虚函数。(子类有重写就用多态)

#include <iostream>
#include <string>
using namespace std;
class Parent
{
public:
    virtual void print()
    {
        cout << "I'm Parent." << endl;
    }
};
class Child : public Parent
{
public:
    void print()                  //  没必要重复写
    {
        cout << "I'm Child." << endl;
    }
};
void how_to_print(Parent* p)
{
    p->print();     // 展现多态的行为
}
int main()
{
    Parent p;
    Child c;    
    how_to_print(&p);    // Expected to print: I'm Parent.
    how_to_print(&c);    // Expected to print: I'm Child.    
    return 0;

}

多态的意义:在程序运行过程中展现出动态的特性。函数重写必须多态实现,否则没有意义。多态是面向对象组件化程序设计的基础特性。

静态联编:在程序的编译期间就能确定具体的函数调用,如函数重载。

动态联编:在程序实际运行后才能确定具体的函数调用,如函数重写。

#include <iostream>
#include <string>
using namespace std;
class Parent
{
public:
    virtual void func()
    {
        cout << "void func()" << endl;
    }    
    virtual void func(int i)
    {
        cout << "void func(int i) : " << i << endl;
    }    
    virtual void func(int i, int j)
    {
        cout << "void func(int i, int j) : " << "(" << i << ", " << j << ")" << endl;
    }
};
class Child : public Parent
{
public:
    void func(int i, int j)        //重写了父类中的两参数函数,实现多态
    {
        cout << "void func(int i, int j) : " << i + j << endl;
    }    
    void func(int i, int j, int k)         //同名覆盖父类中的三个函数,与上一个函数重载
    {
        cout << "void func(int i, int j, int k) : " << i + j + k << endl;
    }
};
void run(Parent* p)
{
    p->func(1, 2);     // 展现多态的特性
                       // 动态联编
}
int main()
{
    Parent p;
    
    p.func();         // 静态联编。编译时确定
    p.func(1);        // 静态联编
    p.func(1, 2);     // 静态联编    
    cout << endl;    
    Child c;    
    c.func(1, 2);     // 静态联编    
    cout << endl;    
    run(&p);     //同一行代码,展现不同行为
    run(&c);    
    return 0;

}

第三个实例:

#include <iostream>
#include <string>
using namespace std;
class Boss
{
public:
    int fight()
    {
        int ret = 10;
        
        cout << "Boss::fight() : " << ret << endl;
        
        return ret;
    }
};
class Master
{
public:
    virtual int eightSwordKill()
    {
        int ret = 8;
        
        cout << "Master::eightSwordKill() : " << ret << endl;
        
        return ret;
    }
};
class NewMaster : public Master
{
public:
    int eightSwordKill()
    {
        int ret = Master::eightSwordKill() * 2;    
        cout << "NewMaster::eightSwordKill() : " << ret << endl;        
        return ret;
    }
};
void field_pk(Master* master, Boss* boss)
{
    int k = master->eightSwordKill();              //Master *master 根据赋值兼容,父子都能用
    int b = boss->fight();    
    if( k < b )
    {
        cout << "Master is killed..." << endl;
    }
    else
    {
        cout << "Boss is killed..." << endl;
    }
}
int main()
{
    Master master;
    Boss boss;    
    cout << "Master vs Boss" << endl;    
    field_pk(&master, &boss);    
    cout << "NewMaster vs Boss" << endl;    
    NewMaster newMaster;    
    field_pk(&newMaster, &boss);    
    return 0;

}

函数重写只可能发生在父类与子类之间,根据实际对象的类型确定调用的具体函数,virtual关键字是c++中支持多态的唯一方式,被重写的虚函数可表现出多态的特性。

猜你喜欢

转载自blog.csdn.net/ws857707645/article/details/80254933