-
说明
-
案例
-
书接上回
- 鞋贩子为了发展企业文化,促进同事之间的关系。
- 组织了一场
CS
真人对战。
-
案例
#include<iostream> #include<string> /************************/ class Guns { public: virtual void shot() = 0; }; class JiaTeLin : public Guns{ public: void shot() { std::cout << "sao she" << std::endl; } }; class M4A1 : public Guns{ public: void shot() { std::cout << "dian she" << std::endl; } }; class AK : public Guns { public: void shot() { std::cout << "bao tou" << std::endl; } }; /************************/ class CSPlayer { public: virtual void PuTongGongJi() = 0; protected: Guns * gun; }; class XiaoShou : public CSPlayer{ public: XiaoShou(Guns *gun) { this->gun = gun; } virtual void PuTongGongJi() { std::cout << "--------- xiaoshou" << std::endl; this->gun->shot(); } }; class DaoGou : public CSPlayer{ public: DaoGou(Guns *gun) { this->gun = gun; } virtual void PuTongGongJi() { std::cout << "--------- dao gou" << std::endl; this->gun->shot(); } }; class ZhiYuan : public CSPlayer { public: ZhiYuan(Guns *gun) { this->gun = gun; } virtual void PuTongGongJi() { std::cout << "--------- zhiyuan" << std::endl; this->gun->shot(); } }; int main() { Guns * ak47 = new AK(); Guns * m4a1 = new M4A1(); Guns * jiatelin = new JiaTeLin(); CSPlayer* zhangsan = new XiaoShou(ak47); CSPlayer* lisi = new DaoGou(m4a1); CSPlayer* wangwu = new ZhiYuan(jiatelin); zhangsan->PuTongGongJi(); lisi->PuTongGongJi(); wangwu->PuTongGongJi(); }
- 这里有不同职位的人参加
CS
,然后为他们配备了不同的武器。 - 职位和武器可以任意组合。
- 这里有不同职位的人参加
-
-
总结
C++ 08 桥接模式
最新推荐文章于 2024-08-28 14:35:43 发布