接口实现
重点内容在本书中的第二章中,介绍了接口的封装例子。
1. 接口定义
C语言中可将接口封装好,让后以.h文件作文扩展,简单例子:
extern int Arith_max(int x, int y);
extern int Arith_min(int x, int y);
extern int Arith_div(int x, int y);
extern int Arith_mod(int x, int y);
extern int Arith_ceiling(int x, int y);
extern int Arith_floor (int x, int y);
上述接口都在arith.h中声明。
以上6个函数都是对两个数的操作,求最大值,求最小值,除法,向上去整,向下取整。个人觉得对于定义接口的话,主要是找对最佳的函数参数,返回值,这一步比较简单。
2. 具体实现
这几个函数的实现比较简单,不过,作为一个接口封装的例子,比较好地诠释了一个过程。
int Arith_max(int x, int y)
{
int max = 0;
if(x >= y)
max = x;
else
max = y;
return