C语言封装成object-c的过程,实现原理
来源:互联网 发布:淘宝信用贷款和网商贷 编辑:程序博客网 时间:2024/05/18 03:14
前言
做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger
,NSObject
,NSString
这些对象是怎么封装的?接下来我们就使用C语言来一部一部的实现这个封装。
Object对象
首先我们先封装一个Object
对象,我们来分析一下:
- 如果使用C来封装对象,我们就要用到结构体
- 每一个
Object
都有一个计数器,这个计数器用来管理对象的释放 - 提供一定的方法,能够retain, release, 获取计数器个数
好了,基于上边的设计呢,我们写了如下的代码:
Object.h
#include "Object.h"// 定义Object对象typedef struct Object { int retainCount;}Object;//宏定义方法 方便书写#define OBJECTRETAIN(obj) objectRetain((Object*)obj)#define OBJECTRELEASE(obj) objectRelease((Object*)obj)#define GETRETAINCOUNT(obj) getRetainCount((Object*)obj)void objectRetain(Object *obj);void objectRelease(Object *obj);int getRetainCount(Object *obj);
接下来,我们只要在.c中实现这三个方法就行了。
Object.c
#include "Object.h"#include <stdlib.h>void objectRetain(Object *obj) { obj -> retainCount += 1;}void objectRelease(Object *obj) { obj -> retainCount -= 1; if (obj -> retainCount <= 0) { free(obj); }}int getRetainCount(Object *obj) { return obj -> retainCount;}
String对象
我们使用C语言的char *来封装String
对象:
String.h
#ifndef String_h#define String_h#include <stdio.h>typedef struct String { int retainCount; char *value;}String;String* newString(char* value);char* getStringValue(String* ins);#endif /* String_h */
String.c
#include "String.h"#include <stdlib.h>#include "Object.h"String* newString(char* value) { String *str = malloc(sizeof(String)); objectRetain((Object *)str); str -> value = value; return str;}char* getStringValue(String* ins) { return ins -> value;}
Integer对象
Integer
是对Int的封装。
Integer.h
#ifndef Integer_h#define Integer_h#include <stdio.h>typedef struct Integer{ int retainCount; int value;}Integer;Integer* newInteger(int value);int getIntegerValue(Integer* ins);#endif /* Integer_h */
Integer.c
#include "Integer.h"#include <stdlib.h>#include "Object.h"Integer *newInteger(int value) { Integer *new = malloc(sizeof(Integer)); OBJECTRETAIN(new); new->value = value; return new;}int getIntegerValue(Integer* ins) { return ins->value;}
People对象
People
对象中有两个属性,一个是String
类型的姓名,一个是Integer
类型的年龄,原理和上边的封装非常相似。
People.h
#ifndef People_h#define People_h#include <stdio.h>#include "Integer.h"#include "String.h"typedef struct People{ int retainCount; String* name; Integer* age;}People;People* newPeople(String *name,Integer *age);String* getName(People* people);Integer* getAge(People* people);#endif /* People_h */
People.c
#include "People.h"#include <stdlib.h>#include "Object.h"People* newPeople(String *name,Integer *age){ People *newP = malloc(sizeof(People)); OBJECTRETAIN(newP); newP->age = age; newP->name = name; return newP;}String* getName(People* people){ return people->name;}Integer* getAge(People* people){ return people->age;}
Array对象
我们上边定义了好几个对象,接下来我们在定义一个数组对象,我们最终的目的是,实现类似OCNSArray的一些简单的功能,这这里我们会把People
放入数组中。
- 数组需要一个参数
length
,用来获取数组中的元素个数 - 还需要一个参数
capacity
,用来说明数组的容量 AnyObject *value
,数组中放着的是一组连续内存的Object对象
代码分析:
//创建数组Array* newArray(){ Array *arr = malloc(sizeof(Array)); arr->length = 0; arr->capacity = 32; arr->value = allocMemoryByCapacity(arr); return arr;}//分配空间static AnyObject* allocMemoryByCapacity(Array *arr){ return malloc(sizeof(People*) * arr->capacity);}
AnyObject是一个对象,他封装了Object *
。malloc函数分配了一段内存后,返回了指向这段内存的指针,也就是AnyObject*
.
在创建Array的时候,value就可以直接使用这个指针进行赋值,同C中的数组是一个概念。
Array.h
#ifndef Array_h#define Array_h#include <stdio.h>#include "People.h"#include "Object.h"typedef Object* AnyObject;typedef struct Array{ int length; int capacity; AnyObject *value;}Array;Array* newArray();//增加数组元素void addElement(Array *array,AnyObject value);//删除Array* removeIndexAt(Array *arry,int index);//插入Array* insertIndexAt(Array *array,AnyObject value,int index);//查找AnyObject getValueIndexAt(Array *array,int index);//获取数组长度int getArrayLength(Array *array);//销毁void destroyArray(Array *array);//打印void printArray(Array *arr);#endif /* Array_h */
Array.c
#include "Array.h"#include <String.h>#include <stdlib.h>#include <assert.h>//分配空间static AnyObject* allocMemoryByCapacity(Array *arr){ return malloc(sizeof(People*) * arr->capacity);}//创建数组Array* newArray(){ Array *arr = malloc(sizeof(Array)); arr->length = 0; arr->capacity = 32; arr->value = allocMemoryByCapacity(arr); return arr;}//获取数组长度int getArrayLength(Array *array){ return array->length;}//增加元素void addElement(Array *array,AnyObject value){ if (array->length >= array->capacity) { array->capacity *= 2; AnyObject *oldValue = array->value; memcpy(array->value, oldValue, array->length*sizeof(AnyObject)); free(oldValue); } OBJECTRETAIN(value); array->value[array->length] = value; array->length++;}//删除元素Array* removeIndexAt(Array *arry,int index){ assert(index >= 0 && index < arry->length); //断言 防止越界 OBJECTRELEASE(getValueIndexAt(arry, index)); arry->length -- ; for (int i = index; i < arry->length; i++) { arry->value[i] = arry->value[i+1]; } return arry;}//在指定位置增加元素Array* insertIndexAt(Array *array,AnyObject value,int index){ if (array->length >= array->capacity) { array->capacity *= 2; AnyObject *oldValue = array->value; memcpy(array->value, oldValue, array->length*sizeof(AnyObject)); free(oldValue); } array->length++; //插入指定位置 array->value[index] = value; //将元素后移 for (int i = index + 1; i < array->length; i++) { array->value[array->length] = array->value[array->length-i]; } OBJECTRETAIN(value); return array;}//获取某个元素AnyObject getValueIndexAt(Array *array,int index){ assert(index >= 0 && index < array->length); return array->value[index];}//销毁void destroyArray(Array *array){ free(array->value); free(array); printf("数组被销毁\n");}//打印结果void printArray(Array *arr){ for (int i = 0; i < arr->length; i++) { printf("位置:%d,姓名:%s,年龄:%d\n",i, getStringValue(getName((People*)getValueIndexAt(arr, i))),getIntegerValue(getAge((People*)getValueIndexAt(arr, i)))); }}
阅读全文
0 0
- C语言封装成object-c的过程,实现原理
- C语言实现封装
- C语言实现封装
- Object-c------类的封装
- C语言实现简单的日志封装
- C语言的编译原理及过程
- C语言实现封装,继承
- object-c通知的原理
- c语言中-x的实现原理
- Object-C语言的优缺点
- 几种语言的单例模式的实现(C++/C#/Object-C)
- C语言实现MAC帧的封装与解封装
- C语言面向对象的实现---封装性
- C语言面向对象的实现---封装性
- C 语言中实现数据与方法的封装
- C 语言中实现数据与方法的封装
- C语言实现C++的封装继承和多态
- C语言是怎样实现封装、继承、多态的?
- 链队列和循环队列的表示与实现-初始化、判空、求长、入队、出队、遍历、求头、求尾、清队、毁队
- Centos 7 和 Centos 6 的区别
- cocos2dx坐标系
- HDU
- C++ 中 public、protected 及 private 的作用总结
- C语言封装成object-c的过程,实现原理
- unity shader入门精要 前3章总结
- Native与html5交互
- 《设计心理学》
- Spring--MVC--如何书写MVC的控制层Controller
- 虚拟机下Linux系统的安装及项目部署详解
- Centos 7 防火墙
- [YTU ]_2736指针练习--输出最大值
- opencv之车牌检测(初)