#define APPLE 1
#define ORANGE 2
class XFruit
{
public :
virtual void ShowName() =
0;
} ;
//派生出子类商品
class Apple :
public XFruit
{
public :
void ShowName (){ShowMessage(
"My name is apple"); }
};
class Orange :
public XFruit
{
public :
void ShowName(){ShowMessage(
"My name is orange");}
} ;
//工厂类基类
class FruitShop
{
public:
XFruit* SaleFruit(
int type)
{
if(type ==
APPLE)
{
return new Apple;
}
else if(type ==
ORANGE)
{
return new Orange;
}
else
{
return NULL;
}
}
} ;
void __fastcall TForm1::RzButton1Click(TObject *
Sender)
{
FruitShop * fruitshop =
new FruitShop();
fruitshop->SaleFruit(APPLE)->
ShowName();
delete fruitshop;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RzButton2Click(TObject *
Sender)
{
FruitShop * fruitshop =
new FruitShop();
fruitshop->SaleFruit(ORANGE)->
ShowName();
delete fruitshop;
}
转载于:https://www.cnblogs.com/luyuxibaby/p/5217290.html