C 语言的面向对象
C 语言的面向对象
如前所说,面向对象是一种软件设计的思想,是语言无关的。在本节中,我举一个链表(list) 的例子来说明如何在 C 语言中的设计出有面向对象风格的代码。
定义接口
接口是面向对象语言中的一个比较重要的概念,接口只对外部承诺实现该接口的实体可以完成什么样的功能,但是不暴露实现的方式。这样的好处是,实现者可以在不接触接口使用者的代码的情况下,对实现进行调整。
我们来看看链表的接口定义:
清单 1. 链表的接口定义
#ifndef _ILIST_H
#define _ILIST_H
// 定义链表中的节点结构
typedef struct node{
void *data;
struct node *next;
}Node;
// 定义链表结构
typedef struct list{
struct list *_this;
Node *head;
int size;
void (*insert)(void *node);// 函数指针
void (*drop)(void *node);
void (*clear)();
int (*getSize)();
void* (*get)(int index);
void (*print)();
}List;
void insert(void *node);
void drop(void *node);
void clear();
int getSize();
void* get(int index);
void print();
#endif /* _ILIST_H */
IList 接口中,可以清晰的看到,对于一个 list 实体 ( 也就是对象 ) 来说,可以在其上 进行 insert, drop, clear, getSize, get(index) 以及 print 等操作。
接口的实现
清单 2. 构造方法
Node *node = NULL;
List *list = NULL;
void insert(void *node);
void drop(void *node);
void clear();
int getSize();
void print();
void* get(int index);
List *ListConstruction(){
list = (List*)malloc(sizeof(List));
node = (Node*)malloc(sizeof(Node));
list->head = node;
list->insert = insert;// 将 insert 函数实现注册在 li
相关文档:
递归链表反序
void Invert(struct node *p)
{
if(p->next==NULL) return;
if(p->next->next!=0)
Invert(p->next);
p->next->next = p;
p-> ......
假设在test.h中定义了一个static bool g_test=false;
若test1.c和test2.c都包含test.h,则test1.c和test2.c分别生成两份g_test,在test1.c 中置g_test=true,而test2.c中仍然为false并未改变!shit!!
一、c程序存储空间布局
C程序一直由下列部分组成:
1)正文段——CPU执行 ......
★★★ C/C++资料大全,拿来分享 ★★★
国内最专业,也是最专一的 C/C++方向资料站 我们的特点是每天都在不断更新
C/C++源码论坛
www.cssdn.net 【 C S S D n】
全国最大的C/C++面试题库,网聚了全球500强IT公司C/C++面试题
http://www.c ......
下面的文件的使用方法是:将下面的文件保存在linux内核源码目录中,文件的名称是.config,然后make menuconfig,在出现
的配置界面中选择Load Configuratio选项,然后可以在这个基础上修改自己的kernel。下面的配置是基于s3c2410,支持yaffs2
文件系统。我看了一下编译出来的uImage大概在800k左右,呵呵么这是本人第一次成 ......
先说python
python的random模块提供了多个伪随机数发生器,默认都是用当前时间戳为随机数种子。
下面是该模块几个最常用的函数
random() Return the next random floating point number in the range [0.0, 1.0).
randint(a,b) Return a random integer N such that a <=
N <= b
randrange([star ......