博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言实现多态
阅读量:6213 次
发布时间:2019-06-21

本文共 7970 字,大约阅读时间需要 26 分钟。

hot3.png

转载自:

 

 

上面一篇博客中写了《》,本文将进一步讲讲如何实现 C 语言的继承和多态,其实大致的思想已经在前面的一篇博客《》中体现过了,主要就是如何灵活运用C的函数指针这一特性!

等等,C本身是不支持继承和多态的,那还煞费苦心实现 C 的继承和多态干嘛呢?其实在 C 的世界里,有一套非常有名的面向对象的框架,用的也非常广,那就是 ,它是整个图形界面开发库 GTK 的基石,在IBM developerWorks上有一篇很好的文章介绍 GObject《》。另外,在 Linux 内核里面也大量使用了面向对象的思想,比如虚拟文件系统,设备驱动等模块,在 上有两篇文章就讲到了内核中的面向对象,详细请看:《》,《》。

好了,貌似扯远了,现在我们就来动手实现C语言的继承与多态,我们还是以比较经典的动物世界中的实例来举例:假设动物们(包括人)都会吃(Eat),会走(Walk),会说(Talk),而派生类为 dog(汪星人) 和 cat(喵星人),当然还可以是更多,dog 和 cat 都有自己独特的 eat, walk 和 talk 方式,那么大致的代码如下:

基类代码 animal-base.h|c:

复制代码

/* * ============================================================================= * *       Filename:  animal-base.h * *    Description:  animal base class. * *        Created:  12/31/2012 11:36:43 AM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#ifndef _ANIMAL_H_#define _ANIMAL_H_typedef struct animal_s_ animal_t;typedef struct animal_ops_s_ animal_ops_t;/* 动物类,是所有动物类的基类,也是抽象类 */struct animal_s_ {    char *name; /*< 动物的名称 */    animal_ops_t *animal_ops; /* 动物的基本行为 */};/* 动物的基本行为 */struct animal_ops_s_ {    /* 动物吃了什么食物 */    void (*eat)(char *food);    /* 动物走了多少步 */    void (*walk)(int steps);    /* 动物在说什么 */    void (*talk)(char *msg);};/* 基类的构造函数,需要显示调用 */extern animal_t * animal_init(char *name);/* 基类的有关操作,如吃,走,说等等 */extern void animal_eat(animal_t *animal, char *food);extern void animal_walk(animal_t *animal, int steps);extern void animal_talk(animal_t *animal, char *msg);/* 基类的析构函数,需要显示调用 */extern void animal_die(animal_t *animal);#endif  /* _ANIMAL_H_ */

复制代码

复制代码

/* * ============================================================================= * *       Filename:  animal-base.c * *    Description:  animal base class. * *        Created:  12/31/2012 12:27:27 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include 
#include
#include
#include "animal-base.h"/* 基类的构造函数,需要显示调用 */animal_t * animal_init(char *name){ assert(name != NULL); size_t name_len = strlen(name); animal_t *animal = (animal_t *)malloc(sizeof(animal_t) + sizeof(animal_ops_t) + name_len + 1); memset(animal, 0, (sizeof(animal_t) + sizeof(animal_ops_t) + name_len + 1)); animal->name = (char *)animal + sizeof(animal_t); memcpy(animal, name, name_len); animal->animal_ops = (animal_ops_t *)((char *)animal + sizeof(animal_t) + name_len + 1); return animal;}/* 基类的有关操作,如吃,走,说等等 */void animal_eat(animal_t *animal, char *food){ animal->animal_ops->eat(food); return;}void animal_walk(animal_t *animal, int steps){ animal->animal_ops->walk(steps); return;}void animal_talk(animal_t *animal, char *msg){ animal->animal_ops->talk(msg); return;}/* 基类的析构函数,需要显示调用 */void animal_die(animal_t *animal){ assert(animal != NULL); free(animal); return;}

复制代码

汪星人 dog 类的实现代码:

复制代码

#include "animal-base.h"typedef struct dog_s_ dog_t;struct dog_s_ {    animal_t base; /* 继承自 animal 基类 */    /* 以下还可以添加与 dog 相关的属性和方法(函数指针), 如: */    /* char *owner; // dog 的主人 */    /* void (*hunt)(const char *rabbit); // 猎兔犬 */};extern dog_t * dog_init();extern void dog_die(dog_t * dog);

复制代码

复制代码

/* * ============================================================================= * *       Filename:  dog.c * *    Description:  dog class derived from animal base class. * *        Created:  12/31/2012 12:52:26 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include 
#include
#include
#include
#include "dog.h"static void eat(char *food);static void walk(int steps);static void talk(char *msg);dog_t * dog_init(){ dog_t *dog = (dog_t *)malloc(sizeof(dog_t)); animal_t *animal = (animal_t *)animal_init("doggggggggggggg"); memcpy(&(dog->base), animal, sizeof(animal_t)); dog->base.animal_ops->eat = eat; dog->base.animal_ops->walk = walk; dog->base.animal_ops->talk = talk; free(animal); return dog;}void dog_die(dog_t *dog){ /* nothing to do here. */}static void eat(char *food){ printf("I'm a dog, I eat %s\n", food);}static void walk(int steps){ printf("I'm a dog, I can jump %d steps one time\n", steps);}static void talk(char *msg){ printf("I'm a dog, I talk my language %s\n", msg);}

复制代码

喵星人(cat 类) 的实现代码:

复制代码

/* * ============================================================================= * *       Filename:  cat.h * *    Description:  cat class derived from animal base class. * *        Created:  12/31/2012 12:44:05 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include "animal-base.h"typedef struct cat_s_ cat_t;struct cat_s_ {    animal_t base; /* 继承自 animal 基类 */    /* 以下还可以添加与 cat 相关的属性和方法(函数指针), 如: */    /* char *owner; // cat 的主人 */    /* void (*hunt)(const char *rabbit); // 猎兔犬 */};extern cat_t * cat_init();extern void cat_die(cat_t * cat);

复制代码

复制代码

/* * ============================================================================= * *       Filename:  cat.c * *    Description:  cat class derived from animal base class. * *        Created:  12/31/2012 12:52:26 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include 
#include
#include
#include
#include "cat.h"static void eat(char *food);static void walk(int steps);static void talk(char *msg);cat_t * cat_init(){ cat_t *cat = (cat_t *)malloc(sizeof(cat_t)); animal_t *animal = (animal_t *)animal_init("cat"); memcpy(&(cat->base), animal, sizeof(animal_t)); cat->base.animal_ops->eat = eat; cat->base.animal_ops->walk = walk; cat->base.animal_ops->talk = talk; free(animal); return cat;}void cat_die(cat_t *cat){ /* nothing to do here. */}static void eat(char *food){ printf("I'm a cat, I eat %s\n", food);}static void walk(int steps){ printf("I'm a cat, I can jump %d steps one time\n", steps);}static void talk(char *msg){ printf("I'm a cat, I talk my language %s\n", msg);}

复制代码

最后,测试代码如下:

复制代码

/* * ============================================================================= * *       Filename:  main.c * *    Description:  main test. * *        Created:  12/31/2012 01:00:43 PM * *         Author:  Fu Haiping (forhappy), haipingf@gmail.com *        Company:  ICT ( Institute Of Computing Technology, CAS ) * * ============================================================================= */#include 
#include "animal-base.h"#include "dog.h"#include "cat.h"int main(int argc, const char *argv[]){ dog_t *dog = dog_init(); cat_t *cat = cat_init(); /* dog 类测试 */ animal_eat(dog, "bones"); animal_walk(dog, 5); animal_talk(dog, "wuang wuang wuang..."); /* cat 类测试 */ animal_eat(cat, "fish"); animal_walk(cat, 3); animal_talk(cat, "miao miao miao...");}

复制代码

当然还有一点点 Makefile 啦:

复制代码

all:mainmain:main.o dog.o cat.o animal-base.o    gcc -o $@ $^main.o:main.ccat.o:cat.cdog.o:dog.canimal-base.o:animal-base.c.PHONY:cleanclean:    rm main main.o dog.o cat.o animal-base.o

复制代码

最后执行结果为:

复制代码

I'm a dog, I eat bonesI'm a dog, I can jump 5 steps one timeI'm a dog, I talk my language wuang wuang wuang...I'm a cat, I eat fishI'm a cat, I can jump 3 steps one timeI'm a cat, I talk my language miao miao miao...

复制代码

查看完整的代码请移步:

 

转载于:https://my.oschina.net/u/4000302/blog/3020339

你可能感兴趣的文章
新maven项目创建JSP出现小红叉报错 javax.servlet.http.HttpServlet not found
查看>>
微信小程序列表加载更多
查看>>
leetcode笔记-1 twosum
查看>>
深浅拷贝
查看>>
sql查询重复记录、删除重复记录方法大全
查看>>
odoo开发笔记 -- 用户配置界面增加模块访问权限
查看>>
instanceof函数内部机制探析
查看>>
linux下phpstorm的快速安装
查看>>
批量删除和批量修改(参数使用list)
查看>>
前端通用框架可行性研究报告之弹窗
查看>>
数据转换
查看>>
IOS在一个程序中启动另一个程序
查看>>
Dubbo初探
查看>>
CDI Features
查看>>
Linux中安装Oracle jdk
查看>>
MFC界面伸缩
查看>>
笔记本搜不到路由无线信号
查看>>
动态规划算法学习总结
查看>>
java 24小时倒计时案例
查看>>
Memcached
查看>>