原来C语言也可以面向对象(一)
来源:互联网 发布:sql 多表查询结果合并 编辑:程序博客网 时间:2024/04/27 14:39
C语言是一门博大精深的语言,我相信没有研读过Linux中代码的人,很少有人体会到吧,废话不多说,简单的写了一个测试demo
test.h
typedef void (* func1_callback)(void);typedef void (* func2_callback)(int i);typedef struct {int size;func1_callback func1_cb;func2_callback func2_cb;} testCallback;typedef struct {int size;int (*init)(testCallback *_tcb);int (*set)(int i);int (*release)(void);} testInterface;
test.c
#include "test.h"#include <stdio.h>testCallback tcb;int test_init(testCallback *_tcb) {printf("This is test init function.\n");tcb = *_tcb;tcb.func1_cb();return 0;}int test_set(int i) {printf("This is test set function, argumaent i is : %d\n", i);tcb.func2_cb(i);return 0;}int test_release(void) {printf("This is test release function.\n");return 0;}static const testInterface tInterface = {.size = sizeof(testInterface),.init = test_init,.set = test_set,.release = test_release,};const testInterface *get_test_interface() {return &tInterface;}
main.c
#include "test.h"#include <stdio.h>void func1_cb() {printf("This is func1 cb, has no argument.\n");}void func2_cb(int i) {printf("This is func2 cb, argument i is : %d\n", i);}testCallback tcb = {sizeof(testCallback),func1_cb,func2_cb,};static const testInterface *ti;int main(void) {ti = get_test_interface();ti->init(&tcb);ti->set(10);ti->release();return 0;}
这个测试demo很简单,不用解释了吧,这篇先这样了。
- 原来C语言也可以面向对象(一)
- C语言也可以“面向对象”
- C语言也面向对象
- C语言也可以“面向对象”—— C语言实现封装、继承和多态
- JAVASCRIPT也可以面向对象
- JAVASCRIPT也可以面向对象
- C语言面向对象
- C语言面向对象
- C和C++的面向对象专题(2)——C语言也能实现面向对象
- 原来我也可以看懂
- 原来C语言也有正则表达式的库
- 用C语言实现面向对象程序设计(一)
- C语言面向对象编程(一):封装与继承
- C语言的面向对象编程(一)
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- Asp.net 2.0 ViewState原理
- wpf 透明窗口拉伸
- ASP.NET MENU控件的一些重要的属性的用法
- Linux常用服务
- cocos2d-x 学习点滴(3) cocos2d-x 支持的开发语言
- 原来C语言也可以面向对象(一)
- matlab读取文本文件
- 阅读nginx源代码---推荐
- 光是想到,在八十岁后
- IsPostBack深入探讨
- 好看的符号大全
- 我觉得处女座一点都不讨嫌
- joomjack database
- Oracle数据库存储过程和存储函数