company.h
#ifndef COMPANY_H
#define COMPANY_H
#include <QtDebug>
class Company
{
public:
Company() {}
void oneDay()
{
getName();
qDebug() << "上班打卡";
qDebug() << "中午休息";
qDebug() << "下班打卡";
}
virtual void getName() = 0;
};
#endif // COMPANY_H
worker.h
#ifndef WORKER_H
#define WORKER_H
#include "company.h"
class Worker : public Company
{
public:
Worker() {}
void getName()
{
qDebug() << "小明";
}
};
#endif // WORKER_H
main
#include <QApplication>
#include "company.h"
#include "worker.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Company *company = new Worker();
company->oneDay();
return a.exec();
}
把相同的操作放到父類中形成一個固定模板,變化的部分放到子類中實現。
UML