以接口实现多态,验证IDE:VC6.0
以接口实现多态研究了两天,使用lw_oopc.h头文件中的宏搞得晕头转向的(PS:也可能是C++、C语言基础不扎实吧嘿嘿,还好意思笑!!!!),想来想去,既然后续会用在嵌入式中,那直接使用C编写了,架构思路还是面向对象,这样即学习了面向对象,又很清楚直观的理解,好了废话不多说,直接上干货!!!
小模块的功能呢,就是售票机中投币识别币的一个功能,通常投币有1元、5元、10元,那么我们如何根据投的币来计算总的投币之和呢?因为我们每投一次币都会有一个面值,暂称value吧,这个value不管是1元币、5元币还是10元币都有的,并且这个value还会被售票机获取并计算,那么我们把这个值可以封装成一个接口,这个接口会被类或者对象使用,那么这个接口就是多态接口。
代码如下:
c14con.h
#ifndef __C14CON_H__
#define __C14CON_H__
typedef struct
{
void (*init)();
double (*value)();
}ICoin_t;
#endif
实现完这个接口,那么就可以实现币类,代码如下:
c14one.h//1元币类
#ifndef __C14ONE_H__
#define __C14ONE_H__
#include "c14con.h"
typedef struct
{
ICoin_t ICoin;
int k;
}one_dollors_t;
one_dollors_t one_dollors;
void *one_dollorsNew();
#endif
c14five.h//5元币类
#ifndef __C14FIVE_H__
#define __C14FIVE_H__
#include "c14con.h"
typedef struct
{
ICoin_t ICoin;
}five_dollors_t;
five_dollors_t five_dollors;
void *five_dollorsNew();
#endif
c14ten.h//10元币类
#ifndef __C14TEN_H__
#define __C14TEN_H__
#include "c14con.h"
typedef struct
{
ICoin_t ICoin;
}ten_dollors_t;
ten_dollors_t ten_dollors;
void *ten_dollorsNew();
#endif
下面分别是币类的实现:
#include <stdio.h>
#include <stdlib.h>
#include "c14one.h"
static void init(){}
static double value()
{
return 1.0;
}
void *one_dollorsNew()
{
one_dollors_t *t;
t = (one_dollors_t *)malloc(sizeof(one_dollors_t));
t->ICoin.init = init;
t->ICoin.value = value;
return (void *)t;
}
#include <stdio.h>
#include <stdlib.h>
#include "c14five.h"
static void init(){};
static double value()
{
return 5.0;
}
void *five_dollorsNew()
{
five_dollors_t *t;
t = (five_dollors_t *)malloc(sizeof(five_dollors_t));
t->ICoin.init = init;
t->ICoin.value = value;
return (void *)t;
}
#include <stdlib.h>
#include "c14ten.h"
static void init(){}
static double value()
{
return 10.0;
}
void *ten_dollorsNew()
{
ten_dollors_t *t;
t = (ten_dollors_t*)malloc(sizeof(ten_dollors_t));
t->ICoin.init = init;
t->ICoin.value = value;
return (void *)t;
}
下面构建售票机类,代码如下:
#ifndef __C14_VM_H__
#define __C14_VM_H__
#include "c14con.h"
typedef struct
{
void (*init)(void *);
void (*feedCoin)(void*, ICoin_t*);
double (*getTotal)(void*);
int index;
ICoin_t* array[10];
}VM_t;
void *vmNew();
#endif
售票机类的元素属性有初始化、接收到的币、计算币的总和、接收币数量、以及存放接收到的币的数组;
实现函数如下:
#include <stdlib.h>
#include "c14vm.h"
static void init(void* t)
{
VM_t* cthis = (VM_t *)t;
cthis->index = 0;
}
static void feedCoin(void* t, ICoin_t* coin)
{
VM_t *cthis = (VM_t*)t;
cthis->array[cthis->index] = coin;
cthis->index++;
}
static double getTotal(void *t)
{
int i;
double sum = 0;
VM_t *cthis = (VM_t*)t;
ICoin_t *pt;
for (i = 0;i < cthis->index;i ++)
{
pt = cthis->array[i];
sum += pt->value();
}
return sum;
}
void *vmNew()
{
VM_t *t;
t = (VM_t *)malloc(sizeof(VM_t));
t->init = init;
t->feedCoin = feedCoin;
t->getTotal = getTotal;
return (void *)t;
}
下面我们将模块进行验证:
#include <stdio.h>
#include <stdlib.h>
#include "c14con.h"
#include "c14five.h"
#include "c14one.h"
#include "c14ten.h"
#include "c14vm.h"
int main(void)
{
ICoin_t *c1,*c5,*c10;
double sum;
VM_t *vm;
vm = (VM_t *)vmNew();
vm->init(vm);
c1 = (ICoin_t *)one_dollorsNew();
c1->init();
vm->feedCoin(vm,c1);
c5 = (ICoin_t *)five_dollorsNew();
c5->init();
vm->feedCoin(vm,c5);
c10 = (ICoin_t *)ten_dollorsNew();
c10->init();
vm->feedCoin(vm,c10);
vm->feedCoin(vm,c1);
vm->feedCoin(vm,c10);
sum = vm->getTotal(vm);
printf("sum is %6.2f\n",sum);
return 0;
使用VC6.0编译结果如下:
完美,不早了,该学英语了、、、、、不板子还没完成、、、、