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

Objective-C的object、class、meta-class

2014-04-09 11:12 459 查看
Objective-c中,什么是object,什么是class?runtime.h中定义的objc_object与objc_class以及NSObject之间究竟是怎样的一种关系?因为apple是个沙盒,一直没弄明白;

参考诸多文档,整理如下:

1.object

一般ooc编程中,object->对象,每一个具有特定属性和特定行为的实体都可称之为对象。对象都有一个类型,每个对象都有自己的属性、行为。

文档objc_object定义:

struct objc_object {
Class isa;
};
也就是说:

Any structure which starts with a pointer to a
Class
structure
can be treated as an
objc_object
.

objective-c中,object的特征:

(1)Every object has a class.在Objective-C中,一个object的class是由isa指针决定,该isa指针指向the
object's Class;

(2)you can send messages to Objective-c objects

2.class
一般ooc中,class的概念,有一些对象是具有相同的结构和特性的。每个对象都属于一个特定的类型;对象的类型,称之为class;
类代表了某一批对象的共性和特征。类是对象的抽象,而对象是类的具体实例(instance)。
c++中类有继承、多态、封装三大特性。

在IOS文档中,class定义如下:

typedef struct objc_class *Class;

struct objc_class {
Class isa;
#if !__OBJC2__
Class super_class /*父类*/;
constchar *name /*类名字*/;
long version /*版本信息*/;
long info /*类信息*/;
long instance_size /*实例大小*/;
struct objc_ivar_list *ivars /*实例变量链表*/;
struct objc_method_list **methodLists /*方法链表*/;
struct objc_cache *cache /*方法缓存链表*/;
struct objc_protocol_list *protocols /*协议链表*/;
#endif
};
Objective-c中,Class 是一个指向类结构体的指针,该类结构体含有一个指向其父类类结构的指针,该类方法的链表,该类方法的缓存以及其他必要信息。

class的特征:
(1)class的定义中,本来就包含有一个指向class data structure 的isa指针,故: [b]every
Class
in
Objective-C is an object itself
.[/b]
<1>class本身就是一个object,那么you can send messages to a class;class发消息时,也可以称之为类对象;
<2>class本身就是一个object,那么它也应该属于一个class,故the
isa pointer of the class自身必须指向一个
Class
structure,从引审出meta-class的概念
<3>既然可以invoke
a method on a class,那么the isa pointer of the class指向的
Class
structure必须contain
the list of
Method
s that we can invoke onthe Class;


(2)class是object的抽象,且因class的继承特性(objective-c中,class废除了多态),那么object's
Class应包含a
list of the Method
s which apply to all objects of that
Class,a
pointer to the superclass
to look up inherited methods;

(3)objective-c class还包含相应的变量列表及protocal列表、以及其它一些必要的信息

类方法调用的例子:

NSStringEncoding
defaultStringEncoding = [NSStringdefaultCStringEncoding];

到此为止,举例来简单说明下objective-c中的消息响应的机制,下篇文章详细分析:

[@"stringValue"writeToFile:@"/file.txt"atomically:YESencoding:NSUTF8StringEncodingerror:NULL];

上例分析:

(1)对一个 Objective-c object(例
NSCFStrin
)发消息时,runtime通过object's
isa
pointer获取the
object's
Class(例NSCFString
class).
该object's
Class包含a
list of the Method
s which apply to all objects of that
Class,a
pointer to the superclass
to look up inherited methods;
(2)runtime遍历the
object's
Class和superclasses的Method
s list,寻找对应的message selector(例
writeToFile:atomically:encoding:error
on
NSString);

(3)the runtime invokes the function
(
IMP
) for that method;

3.meta-class
meta-class,因为是runtime调用,很多人或许没听过;
关于meta-class,苹果官网文档有一段这样叙述:
The compiler
also builds a meta-class object for each class. It describes the class object just as the class object describes instances of the class. But while you can send messages to instances and to the class object, the meta-class object is used only internally by
the runtime system.
meta-class,有人译成元类、原始类等等,这里暂定为元类,但更多直接用meta-class;
上一段中,已经引审出meta-class的定义:
the meta-class is the class for a
Class
object.

关于meta-class的定义,没有文档中找到;

综合以上述class及object定义:

When you send a message to an object, that message is looked up in the method list on the object's class.
When you send a message to a class, that message is looked up in the method list on the class' meta-class.

Meta-class是必要的,因为它用来存储一个类的类方法;
There must be a unique meta-class for every
Class
because
every
Class
has a potentially unique list of class
methods.


meta-class的特征:

(1)The meta-class is the class for a
Class
object
.Every
Class
has
its own unique meta-class (since every
Class
can have its own unique list of methods)
.

(2)All meta-classes use the base class' meta-class (the meta-class of the top
Class
in
their inheritance hierarchy) as their class
.

这个特征是从一个英文文章中看到的,但也从另一篇objective-c内存布局中得到佐证

<ps:>这里再引审下:所有NSObject的子类的meta-class都以NSObject
的 meta-class作为自己的类;

(3)any base meta-classes will be its own class (their
isa
pointer
points to themselves)

(4)the
isa
pointer
on the
NSObject
meta-class points to itself (it is an instance of
itself)

4.classes 和meta-class的继承树

(1)class通过它的super_class指针指向它的父类

(2)meta-class通过它的super_class指向它的父类的meta-class

(3)根类的meta-class的super_class指针,指向根类自己;

(4)all instances, classes and meta-classes in the hierarchy inherit from the hierarchy's base class.

(5)For all instances, classes and meta-classes in the
NSObject
hierarchy,this
means that all
NSObject
instance methods are valid,For the classes and meta-classes, all
NSObject
class methods are also valid.

(6)The meta-class will always ensure that the
Class
object
has all the instance and class methods of
the base class in the hierarchy, plus all of the class methods in-between. For classes descended from
NSObject
,
this means that all the
NSObject
instance and protocol methods are defined for all
Class
(and
meta-class) objects.

(7)All meta-classes themselves use the base class' meta-class (
NSObject
meta-class
for
NSObject
hierarchy classes) as their class,
including the base level meta-class which is the only self-defining class in the runtime.


借用两张图来表示下,继承树中object、class、superclass、meta-class的关系:

继承关系如下:



相应的继承树中,object、class、superclass、meta-class关系如下:



经过查阅多种资料,以及亲手测试,终于把objc_object、objc_class、meta-class的关系搞懂,本文引用很多其他资源,非常感谢,重要资料来源如下:
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html http://www.cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html
/article/5507184.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: