面向对象之继承,封装,多态c语言实现

来源:互联网 发布:巴西足球知乎 编辑:程序博客网 时间:2024/05/18 16:18

最近学习下,c语言实现 面向对象中的 继承,封装,多态方法。


继承:


#include <stdio.h>typedef struct _parent{    int dwparent;}Parent;typedef struct _child{  struct _parent childparent;  int child;}Child;void fun(Parent *ptparent){  printf("the value : %d \n",((Parent*) ptparent)->dwparent);}; void main(void){    Child tchild;   Parent tparent;   tchild.childparent.dwparent  = 222;   tchild.child = 222;tparent.dwparent = 111;fun((Parent*)&tchild);fun((Parent*)&tparent);return;}

封装:

#include <stdio.h>struct tag_t;typedef void (*funhanle)(struct tag_t *);typedef struct tag_t{    int flag;    funhanle handle;}T_Type;#define FUN_PRINT(A) printf("the flag is %d \n", A)void fun_1(T_Type *ptType){    ptType->flag = 1;    FUN_PRINT(ptType->flag);}void fun_2(T_Type *ptType){    ptType->flag = 2;    FUN_PRINT(ptType->flag);}void bulidType(T_Type *pt, funhanle pfun){pt->handle = pfun;}void main(){T_Type  t;    bulidType(&t, fun_1);    t.handle(&t);    printf("t : flag %d\n", t.flag);    bulidType(&t, fun_2);    t.handle(&t);    printf("t : flag %d\n", t.flag);    return;}



多态:

#include <stdio.h>struct _play;void (*pfunHandle)(struct _play *ptfun);typedef struct _play{    void *pData;    pfunHandle pfun;}Play;


参考资料:http://blog.csdn.net/feixiaoxing/article/details/7192302


原创粉丝点击