您的位置:首页 > 移动开发 > Objective-C

C语言封装成object-c的过程,实现原理

2017-06-04 14:18 429 查看


前言

做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))));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: