OOPC_Polymorphism

以接口实现多态,验证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编译结果如下:
在这里插入图片描述完美,不早了,该学英语了、、、、、不板子还没完成、、、、

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeartRain_大西瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值