2019-11-10 02:50:30 +00:00
|
|
|
|
#ifndef __DEMO_H__
|
|
|
|
|
#define __DEMO_H__
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 抽象类(基类)
|
2019-11-10 02:50:30 +00:00
|
|
|
|
class AbstractClass
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-04-04 13:09:08 +00:00
|
|
|
|
virtual ~AbstractClass(){}
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 模板方法,定义一个算法的框架流程
|
2019-11-10 02:50:30 +00:00
|
|
|
|
void templateMethod(){
|
|
|
|
|
// do something
|
|
|
|
|
method1();
|
|
|
|
|
method2();
|
|
|
|
|
method3();
|
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 基本方法——公共方法
|
2019-11-10 02:50:30 +00:00
|
|
|
|
void mehtod1(){
|
|
|
|
|
// do something
|
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 基本方法2
|
2019-11-10 02:50:30 +00:00
|
|
|
|
virtual void method2() = 0;
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 基本方法3——默认实现
|
2019-11-10 02:50:30 +00:00
|
|
|
|
void mehtod3(){
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 具体类(派生类)
|
2019-11-10 02:50:30 +00:00
|
|
|
|
class ConcreteClass :public AbstractClass
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 实现基本方法2
|
2019-11-10 02:50:30 +00:00
|
|
|
|
void method2(){
|
|
|
|
|
// do something
|
|
|
|
|
}
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 重定义基本方法3,覆盖基类的方法3
|
2019-11-10 02:50:30 +00:00
|
|
|
|
void method3(){
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|