c++ 多态

#ifndef PLUGIN_SERVICE_INTERFACE_H
#define PLUGIN_SERVICE_INTERFACE_H

#include <QObject>
#include <QString>

class  PluginServiceInterface
{
public:
    virtual ~PluginServiceInterface() = default;
    virtual void activation(const QString& key = QString(), int requestType = -1) = 0;
};

#endif // PLUGIN_SERVICE_INTERFACE_H
#include "pluginserviceinterface.h"

#include <QObject>

class ActiveCodeServiceImpl : public QObject, public PluginServiceInterface
{
    Q_OBJECT

public:
    explicit ActiveCodeServiceImpl(QObject *parent = Q_NULLPTR);
    ~ActiveCodeServiceImpl() Q_DECL_OVERRIDE;

    void activation(const QString& key = QString(), int requestType = -1) Q_DECL_OVERRIDE;
};
#include "activecode.h"

#include <QDebug>

ActiveCodeServiceImpl::ActiveCodeServiceImpl(QObject *parent)
{

}

ActiveCodeServiceImpl::~ActiveCodeServiceImpl()
{

}

void ActiveCodeServiceImpl::activation(const QString &key, int requestType)
{
    qInfo() << "this is activecode active..." << key;
}
#include "activefile.h"

#include <QDebug>

ActiveFileServiceImpl::ActiveFileServiceImpl(QObject *parent)
{

}

ActiveFileServiceImpl::~ActiveFileServiceImpl()
{

}

void ActiveFileServiceImpl::activation(const QString &key, int requestType)
{
    qInfo() << "this is activefile active..." << key;
}
#include "widget.h"
#include "activecode.h"
#include "activefile.h"

#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    PluginServiceInterface * pluginCode = new ActiveCodeServiceImpl;
    pluginCode->activation();

    qInfo() << endl;
    PluginServiceInterface * pluginFile = new ActiveFileServiceImpl;
    pluginFile->activation();

    return app.exec();
}

c++的多态是c++的三大特性中一个非常重要的性质;

1.封装:看你类设计的咋样,对内和对外接口是否分明,是否完备,能否满足业务的需求;

2.继承:复用,没必要再造轮子;

3.多态:不同点对象根据自己的实际需要实现自己的功能;

        多态分为:编译时多态和运行时多态,编译时是通过函数重载实现的,运行时多态是通过虚函数实现的;

运行时需要具备两个特性:

1.有继承关系;

2.子类重写父类中的虚方法;

虚函数又对应纯虚函数 virtual void fun() = 0;

那么只要含有一个及以上的纯虚函数叫做抽象类,抽象类是不能实例化对象的。

========================================================================

对初学者来说一定要多思考,某种特性出现一定有一定的先进性,主要是针对那种问题提出的,没有这个特性之前,和有了这个特性后提升了什么;

多态主要是为了以后拓展方便,避免牵一发而动全身,主要遵循开放封闭原则: 对扩展开放,对更改封闭;

下面举个例子:

没有多态前:

#include <QApplication>
#include <QDebug>
#include <string>

using std::string;

class Calc{
public:
    int getResult(const string &oper)
    {
        if(oper == "+") {
            return m_value1 + m_value2;
        } else if(oper == "-") {
            return m_value1 - m_value2;
        } else if(oper == "*") {
            return m_value1 * m_value2;
        }
    }

    int m_value1;
    int m_value2;

};
void test1(Calc &c){
    qInfo() << c.m_value1 << "+" << c.m_value2 << "=" << c.getResult("+");
    qInfo() << c.m_value1 << "-" << c.m_value2 << "=" << c.getResult("-");
    qInfo() << c.m_value1 << "*" << c.m_value2 << "=" << c.getResult("*");
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Calc calc;
    int m = 20;
    int n = 10;
    calc.m_value1 = m;
    calc.m_value2 = n;
    test1(calc);

    return a.exec();
}

输出结果:

 

试想后面增加个除法运算得动算法类,而且算法多的话,得把之前的理一遍,维护起来特别不方便。

下面用多态实现: 

#include <QApplication>
#include <QDebug>
#include <string>

using std::string;

class Calc{
public:
    virtual int getResult() =0;
    int m_value1;
    int m_value2;
};

class Add : public Calc
{
public:
    int getResult()
    {
        return m_value1 + m_value2;
    }
};

class Sub : public Calc
{
public:
    int getResult()
    {
        return m_value1 - m_value2;
    }
};

class Mul : public Calc
{
public:
    int getResult()
    {
        return m_value1 * m_value2;
    }
};

void test2(Calc *c, const string &oper){
    qInfo() << c->m_value1 << oper.c_str() << c->m_value2 << "=" << c->getResult();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    int m = 20;
    int n = 10;
    Add add;
    add.m_value1 = m;
    add.m_value2 = n;
    test2(&add, "+");
    Sub sub;
    sub.m_value1 = m;
    sub.m_value2 = n;
    test2(&sub, "-");
    Mul mul;
    mul.m_value1 = m;
    mul.m_value2 = n;
    test2(&mul, "*");

    return a.exec();
}

效果如下:

后面如果增加个除法非常的方便,而不用动其他的算法,只用关注新增的如何实现。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值