今天來學習一下擴充卡模式,下面看一下C++的實作。
//擴充卡模式
class Player
{
public:
virtual void Attack()
{
return;
}
virtual void Defense()
{
return;
}
string s_name;
};
class Forwards : public Player
{
public:
Forwards(string name)
{
s_name = name;
}
void Attack()
{
cout << s_name << "," << "Attack" << endl;
}
void Defense()
{
cout << s_name << "," << "Defense" << endl;
}
};
class ForwardCentor
{
public:
void jingong()
{
cout << "進攻" << endl;
}
void fangshou()
{
cout << "防守" << endl;
}
};
class Translator : public Player
{
public:
Translator(string name)
{
wjzf = new ForwardCentor();
s_name = name;
}
void Attack()
{
cout << s_name;
wjzf->jingong();
}
void Defense()
{
cout << s_name ;
wjzf->fangshou();
}
private:
ForwardCentor *wjzf;
};
int main()
{
Player *b = new Forwards("kebi");
b->Attack();
Player *ym = new Translator("yaoming");
ym->Attack();
delete b;
delete ym;
return 0;
}