C语言实现面向对象的思想(实现封装、继承和多态)

由于C语言是面向过程的语言,在处理比较大的项目时,结构上会显得有些松散。管理起来不免力不从心。

其实在使用C语言写程序的过程中,也可以引入一些面象对象的思想。下面我们主要来谈谈如何用C语言把这些思想表达出来:

1. 封装

这个最简单了,C语言中虽然没有类,但有struct。这可是个好东西。我们可以在一个struct中存入数据和函数指针,以此来模拟类行为。

  1. typedef struct _Parent{  
  2.     int a;  
  3.     int b;  
  4.     void (*print)(struct _Parent *This);  
  5. }Parent;  
typedef struct _Parent{
    int a;
    int b;
    void (*print)(struct _Parent *This);
}Parent;

2.继承

如果要完全地用C语言实现继承,可能有点难度。但如果只是简单的做一下,保证子类中含有父类中的所有成员。这还是不难的。

  1. typedef struct _Child{  
  2.     Parent parent;  
  3.     int c;  
  4. }Child;  
typedef struct _Child{
    Parent parent;
    int c;
}Child;

3. 多态

这个特性恐怕是面向对象思想里面最有用的了。

要用C语言实现这个特性需要一点点技巧,但也不是不可能的。

我们使用上面定义的两个结构体Parent, Child。简单地描述了一个多态的例子。

  1. void print_parent(Parent *This)  
  2. {  
  3.     printf("a = %d. b = %d.\n",  
  4.             This->a, This->b);  
  5. }  
  6.   
  7. void print_child(Parent *This)  
  8. {  
  9.     Child *p = (Child *)This;  
  10.     printf("a = %d. b = %d. c = %d.\n",  
  11.             p->parent.a, p->parent.b, p->c);  
  12. }  
  13.   
  14. Parent *create_parent(int a, int b)  
  15. {  
  16.     Parent *This;  
  17.   
  18.     This = NULL;  
  19.     This = (Parent *)malloc(sizeof(Parent));  
  20.     if (This != NULL){  
  21.         This->a = a;  
  22.         This->b = b;  
  23.         This->print = print_parent;  
  24.         printf("Create parent successfully!\n");  
  25.     }  
  26.       
  27.     return This;  
  28. }  
  29.   
  30. void destroy_parent(Parent **p)  
  31. {  
  32.     if (*p != NULL){  
  33.         free(*p);  
  34.         *p = NULL;  
  35.         printf("Delete parent successfully!\n");  
  36.     }  
  37. }  
  38.   
  39. Child *create_child(int a, int b, int c)  
  40. {  
  41.     Child *This;  
  42.   
  43.     This = NULL;  
  44.     This = (Child *)malloc(sizeof(Child));  
  45.     if (This != NULL){  
  46.         This->parent.a = a;  
  47.         This->parent.b = b;  
  48.         This->c = c;  
  49.         This->parent.print = print_child;  
  50.         printf("Create child successfully!\n");  
  51.     }  
  52.       
  53.     return This;  
  54. }  
  55.   
  56. void destroy_child(Child **p)  
  57. {  
  58.     if (*p != NULL){  
  59.         free(*p);  
  60.         *p = NULL;  
  61.         printf("Delete child successfully!\n");  
  62.     }  
  63. }  
  64.   
  65. int main()  
  66. {  
  67.     Child *p = create_child(1, 2, 3);  
  68.     Parent *q;  
  69.   
  70.     /*Use parent pointer to point to child*/  
  71.     q = (Parent *)p;  
  72.     /*Be attention!*/  
  73.     /*Actually the child's print function is called!*/  
  74.     q->print(q);  
  75.   
  76.     destroy_child(&p);  
  77.     return 0;  
  78.   
  79. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值