#include<iostream>
using namespace std;
class Car
{
public:
virtual void show() = 0;
};
class RunCar: public Car
{
public:
virtual void show()
{
cout << "I'm can run." << endl;
}
};
class FlyCar: public Car
{
public:
FlyCar(Car *car)
{
m_car= car;
}
virtual void show()
{
m_car->show();
cout << "I'm can fly." << endl;
}
Car *m_car;
};
class SwimCar: public Car
{
public:
SwimCar(Car *car)
{
m_car = car;
}
virtual void show()
{
m_car->show();
cout << "I'm can swim." << endl;
}
public:
Car *m_car;
};
int main()
{
Car *runcar = new RunCar;
runcar->show();
cout << "---------------" << endl;
Car *flycar = new FlyCar(runcar);
flycar->show();
cout << "---------------" << endl;
Car *swimcar = new SwimCar(flycar);
swimcar->show();
cout << "---------------" << endl;
return 0;
}
设计模式之-----装饰模式
最新推荐文章于 2024-09-23 11:02:24 发布