64,653
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
class Tank
{
public:
virtual void Shot();
virtual void Run();
};
class T50:public Tank
{
public:
T50()
{ }
public:
void Shot(){ printf("T50 is shoting\n");}
void Run() { printf("T50 is running\n");}
};
class Decorator: public Tank
{
public:
Tank *tank;
public:
Decorator(Tank *tank)
{
this->tank=tank;
}
virtual void Shot();
virtual void Run();
};
class DecoratorA:public Decorator
{
public:
DecoratorA(Tank *tank)
{
this->tank=tank;
}
void Shot()
{
//..功能的扩展
printf("The tank has DecoratorA Shot() function\n");
this->Shot();
}
void Run()
{ //..功能扩展
printf("The tank has DecoratorA Run() function\n");
this->Run();
}
};
void main()
{
Tank *tank = new T50();
DecoratorA *da= new DecoratorA(tank);
da->Shot();
getchar();
}