zoukankan      html  css  js  c++  java
  • C之面向对象编程20170707

    语言只是工具,设计思维才是根本。C虽然是面向过程的语言,但也是可以实现面向对象编程的,本文就是介绍如何使用C语言实现面向对象编程。

    我们知道面向对象主要有三大特性:封装,继承,和多态,下面就从这个三个方面讲解:

    一、封装:

    个人认为封装最重要的意义是:通过类的公有函数访问私有成员,从而降低耦合。

    C中我们通过结构体加函数指针的方式实现封装,我们直接看代码:

    封装使用场景:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeEncapsulation.c
     5 * Description        :     封装    
     6 book@book-desktop:/work/projects/test$ gcc -o RealizeEncapsulationTest RealizeEncapsulation.c RealizeEncapsulationNoStaticVariable.c RealizeEncapsulationTest.c
     7 book@book-desktop:/work/projects/test$ ./RealizeEncapsulationTest 
     8 GetIP:218.17.161.66,Port:21213
     9 AfterSet,IP:192.168.12.1,Port:12322
    10 AfterSetValue,IP:192.168.1.1,Port:12222
    11 
    12 
    13 * Created            :     2017.07.02.
    14 * Author            :     Yu Weifeng
    15 * Function List         :     
    16 * Last Modified     :     
    17 * History            :     
    18 ******************************************************************************/
    19 #include"stdio.h"
    20 #include"malloc.h"
    21 #include"stdlib.h"
    22 #include"string.h"
    23 #include"RealizeEncapsulation.h"
    24 #include"RealizeEncapsulationNoStaticVariable.h"
    25 
    26 #define STRING_MAX_LEN    (15)
    27 /*****************************************************************************
    28 -Fuction        : main
    29 -Description    : main
    30 -Input            : 
    31 -Output         : 
    32 -Return         : 
    33 * Modify Date      Version         Author           Modification
    34 * -----------------------------------------------
    35 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    36 ******************************************************************************/
    37 int main(int argc,char **argv)
    38 {
    39     char strIp[STRING_MAX_LEN];
    40     unsigned short wPort;
    41     //缺陷:多个对象访问的是同一个私有变量(所以并发要加锁)
    42     T_ParaOpr tParaOpr=newParaOpr;//创建实例化对象
    43     tParaOpr.GetPara(strIp,STRING_MAX_LEN,&wPort);
    44     printf("GetIP:%s,Port:%d
    ",strIp,wPort);
    45     
    46     strncpy(strIp,"192.168.12.1",sizeof("192.168.12.1"));
    47     wPort=12322;
    48     tParaOpr.SetPara(strIp,wPort);
    49     tParaOpr.GetPara(strIp,STRING_MAX_LEN,&wPort);
    50     printf("AfterSet,IP:%s,Port:%d
    ",strIp,wPort);
    51     //私有变量在外部,虽然没静态但破坏了封装性,所以上一种更好
    52     char strIpValue[STRING_MAX_LEN];
    53     unsigned short wPortValue;
    54     T_NetParaOpr tNetParaOpr=newNetParaOpr(strIpValue,wPortValue);//创建实例化对象    ,传入变量
    55     strncpy(strIp,"192.168.1.1",sizeof("192.168.1.1"));
    56     wPort=12222;
    57     tNetParaOpr.SetPara(&tNetParaOpr,strIp,wPort);
    58     tNetParaOpr.GetPara(&tNetParaOpr,strIp,&wPort);
    59     printf("AfterSetValue,IP:%s,Port:%d
    ",strIp,wPort);
    60     return 0;    
    61 }
    RealizeEncapsulationTest.c

    被调用者:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeEncapsulation.c
     5 * Description        :     整个文件只包含类相关的变量及操作
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #include"stdio.h"
    13 #include"malloc.h"
    14 #include"stdlib.h"
    15 #include"string.h"
    16 #include"RealizeEncapsulation.h"
    17 
    18 #define IP_MAX_LEN    (15)
    19 
    20 static char g_strIp[IP_MAX_LEN];//私有变量
    21 static unsigned short g_wPort;//私有变量
    22 
    23 /*****************************************************************************
    24 -Fuction        : InitPara
    25 -Description    : 私有函数
    26 -Input            : 
    27 -Output         : 
    28 -Return         : 
    29 * Modify Date      Version         Author           Modification
    30 * -----------------------------------------------
    31 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    32 ******************************************************************************/
    33 static void InitPara()
    34 {
    35     //多线程可内部加锁
    36     strncpy(g_strIp,"218.17.161.66",IP_MAX_LEN);
    37     g_wPort=21213;
    38 }
    39 
    40 /*****************************************************************************
    41 -Fuction        : SetPara
    42 -Description    : 公有函数
    43 -Input            : 
    44 -Output         : 
    45 -Return         : 
    46 * Modify Date      Version         Author           Modification
    47 * -----------------------------------------------
    48 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    49 ******************************************************************************/
    50 void SetPara(char *i_strIp,unsigned short i_wPort)
    51 {
    52     //多线程可内部加锁
    53     strncpy(g_strIp,i_strIp,IP_MAX_LEN);
    54     g_wPort=i_wPort;
    55 }
    56 /*****************************************************************************
    57 -Fuction        : GetPara
    58 -Description    : 公有函数
    59 -Input            : 
    60 -Output         : 
    61 -Return         : 
    62 * Modify Date      Version         Author           Modification
    63 * -----------------------------------------------
    64 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    65 ******************************************************************************/
    66 void GetPara(char *o_strIp,char i_strIpMaxLen,unsigned short *o_wPort)
    67 {
    68     //多线程可内部加锁
    69     if(NULL==g_strIp||g_wPort==0)
    70     {
    71         InitPara();
    72     }
    73     else
    74     {
    75     }
    76     strncpy(o_strIp,g_strIp,i_strIpMaxLen);
    77     *o_wPort=g_wPort;
    78 }
    RealizeEncapsulation.c
     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeEncapsulation.h
     5 * Description        :     RealizeEncapsulation operation center
     6 * Created            :     2017.06.24.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #ifndef _REALIZE_ENCAPSULATION_H
    13 #define _REALIZE_ENCAPSULATION_H
    14 
    15 
    16 void SetPara(char *i_strIp,unsigned short i_wPort);//公有函数声明
    17 void GetPara(char *o_strIp,char i_strIpMaxLen,unsigned short *o_wPort);//公有函数声明
    18 #define newParaOpr {SetPara,GetPara} //类似构造函数
    19 
    20 
    21 typedef struct ParaOpr
    22 {
    23     void (*SetPara)(char *i_strIp,unsigned short i_wPort);
    24     void (*GetPara)(char *o_strIp,char i_strIpMaxLen,unsigned short *o_wPort);
    25 }T_ParaOpr;//定义类
    26 
    27 
    28 
    29 #endif
    RealizeEncapsulation.h
     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeEncapsulationNoStaticVariable.c
     5 * Description        :     整个文件只包含类相关的操作
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #include"stdio.h"
    13 #include"malloc.h"
    14 #include"stdlib.h"
    15 #include"string.h"
    16 #include"RealizeEncapsulationNoStaticVariable.h"
    17 
    18 
    19 #define NET_IP_MAX_LEN    (15)
    20 /*****************************************************************************
    21 -Fuction        : InitNetPara
    22 -Description    : 私有函数
    23 -Input            : 
    24 -Output         : 
    25 -Return         : 
    26 * Modify Date      Version         Author           Modification
    27 * -----------------------------------------------
    28 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    29 ******************************************************************************/
    30 static void InitNetPara(char *i_strIp,unsigned short *i_pwPort)
    31 {
    32     strncpy(i_strIp,"218.17.161.66",NET_IP_MAX_LEN);
    33     *i_pwPort=21213;
    34 }
    35 
    36 /*****************************************************************************
    37 -Fuction        : SetNetPara
    38 -Description    : 公有函数
    39 -Input            : 
    40 -Output         : 
    41 -Return         : 
    42 * Modify Date      Version         Author           Modification
    43 * -----------------------------------------------
    44 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    45 ******************************************************************************/
    46 void SetNetPara(T_NetParaOpr *i_ptNetParaOpr,char *i_strIp,unsigned short i_wPort)
    47 {
    48     strncpy(i_ptNetParaOpr->strIp,i_strIp,NET_IP_MAX_LEN);
    49     i_ptNetParaOpr->wPort=i_wPort;
    50 }
    51 /*****************************************************************************
    52 -Fuction        : GetNetPara
    53 -Description    : 公有函数
    54 -Input            : 
    55 -Output         : 
    56 -Return         : 
    57 * Modify Date      Version         Author           Modification
    58 * -----------------------------------------------
    59 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    60 ******************************************************************************/
    61 void GetNetPara(T_NetParaOpr *i_ptNetParaOpr,char *o_strIp,unsigned short *o_wPort)
    62 {
    63     if(NULL==i_ptNetParaOpr->strIp||i_ptNetParaOpr->wPort==0)
    64     {
    65         InitNetPara(i_ptNetParaOpr->strIp,&i_ptNetParaOpr->wPort);
    66     }
    67     else
    68     {
    69     }
    70     strncpy(o_strIp,i_ptNetParaOpr->strIp,NET_IP_MAX_LEN);
    71     *o_wPort=i_ptNetParaOpr->wPort;
    72 }
    RealizeEncapsulationNoStaticVariable.c
     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeEncapsulationNoStaticVariable.h
     5 * Description        :     RealizeEncapsulationNoStaticVariable operation center
     6 * Created            :     2017.06.24.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #ifndef _REALIZE_ENCAPSULATION_NO_STATIC_VARIABLE_H
    13 #define _REALIZE_ENCAPSULATION_NO_STATIC_VARIABLE_H
    14 
    15 
    16 
    17 
    18 typedef struct NetParaOpr
    19 {
    20     char *strIp;//也可以使用空指针,函数使用时进行转换,
    21     //但会导致隐含条件,外部传入的类型必须与内部使用的类型一致
    22     unsigned short wPort;//变量放在外面,暴露了数据,破坏了封装性
    23     void (*SetPara)(struct NetParaOpr *i_ptNetParaOpr,char *i_strIp,unsigned short i_wPort);
    24     void (*GetPara)(struct NetParaOpr *i_ptNetParaOpr,char *o_strIp,unsigned short *o_wPort);
    25 }T_NetParaOpr;//定义类
    26 
    27 void SetNetPara(T_NetParaOpr *i_ptNetParaOpr,char *i_strIp,unsigned short i_wPort);//公有函数声明
    28 void GetNetPara(T_NetParaOpr *i_ptNetParaOpr,char *o_strIp,unsigned short *o_wPort);//公有函数声明
    29 #define newNetParaOpr(Ip,Port) {Ip,Port,SetNetPara,GetNetPara} //类似构造函数
    30 
    31 
    32 
    33 
    34 #endif
    RealizeEncapsulationNoStaticVariable.h

    运行看一下:

    book@book-desktop:/work/projects/test$ gcc -o RealizeEncapsulationTest RealizeEncapsulation.c RealizeEncapsulationNoStaticVariable.c RealizeEncapsulationTest.c

    book@book-desktop:/work/projects/test$ ./RealizeEncapsulationTest

    GetIP:218.17.161.66,Port:21213

    AfterSet,IP:192.168.12.1,Port:12322

    AfterSetValue,IP:192.168.1.1,Port:12222

    代码中展示了两种实现方式,但显然第一种有静态的更好,封装性更强。

    显然,除了上述两种,还有一种方式,即把私有变量放到结构体中也就是类中,但是这样就不存在私有的说法了,外部都可以访问,除
    非有团队制度规定不允许访问类中的成员变量(开源出去就不行了,显然不可能所有人都有遵循这个制度),所以这种方
    式一般情况下也不太可行。

    二、继承:

    继承的意义在于代码的复用,子类可以复用父类的代码,从而消除重复,当然子类可以有自己的个性。

    C中我们通过结构体中再定义一个结构体,即类里面再定义一个类的方式实现,直接看代码:

    继承使用场景:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeInheritanceTest.c
     5 * Description        :     继承
     6 
     7 book@book-desktop:/work/projects/test$ gcc -o RealizeInheritanceTest RealizeEncapsulation.c RealizeInheritance.c RealizeInheritanceTest.c 
     8 book@book-desktop:/work/projects/test$ ./RealizeInheritanceTest 
     9 FatherGetIP:218.17.161.66,Port:21213
    10 Son:OOP_V1.0.0
    11 
    12 * Created            :     2017.07.02.
    13 * Author            :     Yu Weifeng
    14 * Function List         :     
    15 * Last Modified     :     
    16 * History            :     
    17 ******************************************************************************/
    18 #include"stdio.h"
    19 #include"malloc.h"
    20 #include"stdlib.h"
    21 #include"string.h"
    22 #include"RealizeInheritance.h"
    23 
    24 #define STRING_MAX_LEN    (15)
    25 /*****************************************************************************
    26 -Fuction        : main
    27 -Description    : main
    28 -Input            : 
    29 -Output         : 
    30 -Return         : 
    31 * Modify Date      Version         Author           Modification
    32 * -----------------------------------------------
    33 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    34 ******************************************************************************/
    35 int main(int argc,char **argv)
    36 {
    37     char strIp[STRING_MAX_LEN];
    38     unsigned short wPort;
    39 
    40     T_ParaOpr tParaOpr=newParaOpr;//创建父类实例化对象
    41     T_VersionParaOpr tVersionParaOpr=newVersionParaOpr(tParaOpr);//创建子类实例化对象
    42     
    43     tVersionParaOpr.tParaOpr.GetPara(strIp,STRING_MAX_LEN,&wPort);//访问父类公有函数
    44     printf("FatherGetIP:%s,Port:%d
    ",strIp,wPort);
    45     tVersionParaOpr.PrintVersion();//访问子类自己的公有函数
    46 
    47     return 0;    
    48 }
    RealizeInheritanceTest.c

    也可以不创建父类的实例化对象,直接创建子类的实例化对象,在new子类对象的宏定义加入父类的实现函数代替传入结构体,这样就
    可以直接new子类了。

    被调用者:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeInheritance.c
     5 * Description        :     整个文件只包含类相关的变量及操作
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #include"stdio.h"
    13 #include"malloc.h"
    14 #include"stdlib.h"
    15 #include"string.h"
    16 
    17 
    18 
    19 static char *g_strSoftVersion="OOP_V1.0.0";
    20 
    21 
    22 /*****************************************************************************
    23 -Fuction        : PrintVersion
    24 -Description    : 公有函数
    25 -Input            : 
    26 -Output         : 
    27 -Return         : 
    28 * Modify Date      Version         Author           Modification
    29 * -----------------------------------------------
    30 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    31 ******************************************************************************/
    32 void PrintVersion()
    33 {
    34     printf("Son:%s
    ",g_strSoftVersion);
    35 }
    RealizeInheritance.c
     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizeInheritance.h
     5 * Description        :     
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #ifndef REALIZE_INHERITANCE_H
    13 #define REALIZE_INHERITANCE_H
    14 #include"RealizeEncapsulation.h"
    15 
    16 void PrintVersion();
    17 
    18 typedef struct VersionParaOpr
    19 {
    20     T_ParaOpr tParaOpr;//继承父类
    21     void (*PrintVersion)();
    22 }T_VersionParaOpr;
    23 #define newVersionParaOpr(ParaOpr) {ParaOpr,PrintVersion}
    24 
    25 
    26 
    27 
    28 
    29 
    30 #endif
    RealizeInheritance.h

    执行结果:

    book@book-desktop:/work/projects/test$ gcc -o RealizeInheritanceTest RealizeEncapsulation.c RealizeInheritance.c RealizeInheritanceTest.c

    book@book-desktop:/work/projects/test$ ./RealizeInheritanceTest

    FatherGetIP:218.17.161.66,Port:21213

    Son:OOP_V1.0.0

    关于super,this关键字的实现:如果有函数成员要使用this或者super,则在该成员函数的传参中加入指向本结构体
    的指针参数

    三、多态:

    多态的意思是,类似的形式,各种不同的状态(具体处理可不同),从而消除重复代码,同时对于外部使用着来说调用的形式类似,可减少外部的修改

    1.函数的多态性

    即函数的重载与覆写,覆写覆盖函数指针即可,至于重载,需要使用可变参数va_args实现参数个数的重载,

    除了根据参数个数实现重载以外,还可以实现参数类型的重载(typeof),这主要是利用了 GCC 的内置函数,__builtin_types_compatible_p()和__builtin_choose_expr()

    不过,从单一职责原则的角度看,函数重载的使用相对较少,同时C 实现函数重载需要开发人员自己编写很多额外的代码,这也使得 C 语言不太适合用函数重载方式来编写规范的应用程序接口。

    原文:http://www.cnblogs.com/haippy/archive/2012/12/27/2835358.html

    2.对象的多态性

    C实现对象多态性,我们对对象二次赋值即可,对象还是那个对象但内部实现已经可有所不同,也就是是对象转型,直接看代码:

    多态使用场景:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizePolymorphismTest.c
     5 * Description        :     多态:类似(几乎相同)的形式,各种不同的状态(具体处理可不同)
     6 book@book-desktop:/work/projects/test$ gcc -o RealizePolymorphismTest RealizeEncapsulation.c RealizePolymorphism.c RealizePolymorphismTest.c 
     7 book@book-desktop:/work/projects/test$ ./RealizePolymorphismTest 
     8 GetPara:13677905555,Port:12346
     9 AfterSet strPara:123454678,wPara:123
    10 * Created            :     2017.07.02.
    11 * Author            :     Yu Weifeng
    12 * Function List         :     
    13 * Last Modified     :     
    14 * History            :     
    15 ******************************************************************************/
    16 
    17 #include"stdio.h"
    18 #include"malloc.h"
    19 #include"stdlib.h"
    20 #include"string.h"
    21 #include"RealizePolymorphism.h"
    22 #include"RealizeEncapsulation.h"
    23 
    24 #define STRING_MAX_LEN    (15)
    25 
    26 /*****************************************************************************
    27 -Fuction        : main
    28 -Description    : main
    29 -Input            : 
    30 -Output         : 
    31 -Return         : 
    32 * Modify Date      Version         Author           Modification
    33 * -----------------------------------------------
    34 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    35 ******************************************************************************/
    36 int main(int argc,char **argv)
    37 {
    38     char strPara[STRING_MAX_LEN]={0};//字符数组需要长度,防止意外风险
    39     unsigned short wPara;
    40 
    41     T_ParaOpr tParaOpr=newParaOpr;//创建实例化对象,
    42     //定义后再初始化需要强制转换一下
    43     tParaOpr=(T_ParaOpr)newSmsParaOpr;//创建子类实例化对象,类似向上转型
    44     
    45     tParaOpr.GetPara(strPara,STRING_MAX_LEN,&wPara);//相同形式,具体使用哪个不相同
    46     printf("GetPara:%s,Port:%d
    ",strPara,wPara);
    47     
    48     strncpy(strPara,"123454678",sizeof("123454678"));
    49     wPara=123;
    50     tParaOpr.SetPara(strPara,wPara);//外部不知道具体处理是哪一个
    51     tParaOpr.GetPara(strPara,STRING_MAX_LEN,&wPara);
    52     printf("AfterSet strPara:%s,wPara:%d
    ",strPara,wPara);
    53 
    54     return 0;    
    55 }
    RealizePolymorphismTest.c

    被调用者:

     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizePolymorphism.c
     5 * Description        :     整个文件只包含类相关的变量及操作
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #include"stdio.h"
    13 #include"malloc.h"
    14 #include"stdlib.h"
    15 #include"string.h"
    16 #include"RealizePolymorphism.h"
    17 
    18 #define PHONE_NUM_MAX_LEN    (12)
    19 
    20 static char g_strPhoneNum[PHONE_NUM_MAX_LEN];//私有变量
    21 static unsigned short g_wDeviceId;//私有变量
    22 
    23 
    24 /*****************************************************************************
    25 -Fuction        : InitSmsPara
    26 -Description    : 私有函数
    27 -Input            : 
    28 -Output         : 
    29 -Return         : 
    30 * Modify Date      Version         Author           Modification
    31 * -----------------------------------------------
    32 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    33 ******************************************************************************/
    34 static void InitSmsPara()
    35 {
    36     //多线程可内部加锁
    37     strncpy(g_strPhoneNum,"13677905555",PHONE_NUM_MAX_LEN);
    38     g_wDeviceId=12346;
    39 }
    40 
    41 /*****************************************************************************
    42 -Fuction        : SetSmsPara
    43 -Description    : 公有函数
    44 -Input            : 
    45 -Output         : 
    46 -Return         : 
    47 * Modify Date      Version         Author           Modification
    48 * -----------------------------------------------
    49 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    50 ******************************************************************************/
    51 void SetSmsPara(char *i_strPhoneNum,unsigned short i_wDeviceId)
    52 {
    53     //多线程可内部加锁
    54     strncpy(g_strPhoneNum,i_strPhoneNum,PHONE_NUM_MAX_LEN);
    55     g_wDeviceId=i_wDeviceId;
    56 }
    57 /*****************************************************************************
    58 -Fuction        : GetSmsPara
    59 -Description    : 公有函数
    60 -Input            : 
    61 -Output         : 
    62 -Return         : 
    63 * Modify Date      Version         Author           Modification
    64 * -----------------------------------------------
    65 * 2017/06/29      V1.0.0         Yu Weifeng       Created
    66 ******************************************************************************/
    67 void GetSmsPara(char *o_strPhoneNum,char i_strPhoneNumMaxLen,unsigned short *o_wDeviceId)
    68 {
    69     //多线程可内部加锁
    70     if(NULL==g_strPhoneNum||g_wDeviceId==0)
    71     {
    72         InitSmsPara();
    73     }
    74     else
    75     {
    76     }
    77     strncpy(o_strPhoneNum,g_strPhoneNum,i_strPhoneNumMaxLen);
    78     *o_wDeviceId=g_wDeviceId;
    79 }
    RealizePolymorphism.c
     1 /*****************************************************************************
     2 * Copyright (C) 2017-2018 Hanson Yu  All rights reserved.
     3 ------------------------------------------------------------------------------
     4 * File Module        :     RealizePolymorphism.h
     5 * Description        :     
     6 * Created            :     2017.07.02.
     7 * Author            :     Yu Weifeng
     8 * Function List         :     
     9 * Last Modified     :     
    10 * History            :     
    11 ******************************************************************************/
    12 #ifndef REALIZE_POLYMORPHISM_H
    13 #define REALIZE_POLYMORPHISM_H
    14 
    15 #include<stdbool.h>
    16 
    17 
    18 void SetSmsPara(char *i_strPhoneNum,unsigned short i_wDeviceId);//公有函数声明
    19 void GetSmsPara(char *o_strPhoneNum,char i_strPhoneNumMaxLen,unsigned short *o_wDeviceId);//公有函数声明
    20 #define newSmsParaOpr {SetSmsPara,GetSmsPara} //类似构造函数
    21 
    22 
    23 typedef struct SmsParaOpr
    24 {
    25     void (*SetSmsPara)(char *i_strPhoneNum,unsigned short i_wDeviceId);//可对操作的传参再封装为结构体,类似于java泛型中的通配符
    26     void (*GetSmsPara)(char *o_strPhoneNum,char i_strPhoneNumMaxLen,unsigned short *o_wDeviceId);//外部并不知道具体操作了结构体中的哪种类型
    27     //即父子类或其他对象可访问结构体的不同部分,外部看起来形式是一致的,并不知道内部使用了哪部分
    28 }T_SmsParaOpr;//定义类
    29 
    30 
    31 
    32 
    33 
    34 
    35 
    36 
    37 #endif
    RealizePolymorphism.h

    执行结果:

    book@book-desktop:/work/projects/test$ gcc -o RealizePolymorphismTest RealizeEncapsulation.c RealizePolymorphism.c RealizePolymorphismTest.c

    book@book-desktop:/work/projects/test$ ./RealizePolymorphismTest

    GetPara:13677905555,Port:12346

    AfterSet strPara:123454678,wPara:123

     详细源码可git clone https://github.com/fengweiyu/oopUseC.git

  • 相关阅读:
    团队项目第一阶段冲刺站立会议(5月10日)
    团队项目第一阶段冲刺站立会议(5月9日)
    团队项目第一阶段冲刺站立会议(5月7日)
    课堂练习之找数字0-N中“1”出现的次数
    团队开发项目-----来用------项目风险分析
    《你的灯亮着吗》阅读笔记之第五篇与第六篇
    《你的灯亮着吗》阅读笔记之第三篇与第四篇
    《你的灯亮着吗》阅读笔记之第一篇与第二篇
    课堂练习之检测水军(拓展)
    课后作业之输入法评价
  • 原文地址:https://www.cnblogs.com/yuweifeng/p/7133287.html
Copyright © 2011-2022 走看看