Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2578310
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: 项目管理

2012-09-30 14:03:38

factory.h

点击(此处)折叠或打开

  1. #ifndef _FACTORY_H_
  2. #define _FACTORY_H_

  3. class Human
  4. {
  5. public:
  6.     virtual void laguage() = 0;
  7. };


  8. class WhiteHuman : public Human
  9. {
  10. public:
  11.     void laguage();
  12. };

  13. class BlackHuman : public Human
  14. {
  15. public:
  16.     void laguage();
  17. };

  18. class YellowHuman : public Human
  19. {
  20. public:
  21.     void laguage();
  22. };

  23. class Factory
  24. {
  25. public:
  26.     Factory()
  27.     {
  28.     }
  29.     ~Factory()
  30.     {
  31.     }
  32. public:
  33.     static Human * getHum(Human * hu);
  34. private:
  35.     static Human * hum; /// < 不是静态的getHum不能直接调用;原因...
  36. };

  37. #endif
factory.c

点击(此处)折叠或打开

  1. #include "factory.h"
  2. #include <iostream>
  3. using namespace std;

  4. void WhiteHuman::laguage()
  5. {
  6.     cout << "我说英语" << endl;
  7. }

  8. void BlackHuman::laguage()
  9. {
  10.     cout << "我说外语" << endl;
  11. }

  12. void YellowHuman::laguage()
  13. {
  14.     cout << "我说汉语" << endl;
  15. }

  16. Human * Factory::hum = NULL;

  17. Human * Factory::getHum(Human * hu)
  18. {
  19.     if (NULL == hu)
  20.         return NULL;
  21.     hum = hu;
  22.     
  23.     return hum;
  24. }
main.c

点击(此处)折叠或打开

  1. #include "factory.h"

  2. int main(int argc, const char *argv[])
  3. {
  4.     Human * white = Factory::getHum(new WhiteHuman());
  5.     white->laguage();
  6.     delete white;

  7.     Human * black = Factory::getHum(new BlackHuman());
  8.     black->laguage();
  9.     delete black;

  10.     Human * yellow = Factory::getHum(new YellowHuman());
  11.     yellow->laguage();
  12.     delete yellow;

  13.     return 0;
  14. }

阅读(918) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~