本文為大地瓜原創(chuàng)状原,歡迎知識(shí)共享聋呢,轉(zhuǎn)載請(qǐng)注明出處。
雖然你不注明出處我也沒(méi)什么精力和你計(jì)較遭笋。
作者微信號(hào):christgreenlaw
本文的原文坝冕。本文只對(duì)其進(jìn)行翻譯。
Objective-C is a class-based object system. Each object is an instance of some class; the object'sisa
pointer points to its class. That class describes the object's data: allocation size and ivar types and layout. The class also describes the object's behavior: the selectors it responds to and instance methods it implements.
Objective-C是一個(gè)基于class的對(duì)象系統(tǒng)质和。每個(gè)對(duì)象都是某個(gè)類的實(shí)例稳摄,對(duì)象的isa
指針指向其類。那個(gè)類描述了對(duì)象的數(shù)據(jù):占用空間大小饲宿、ivar類型以及樣式厦酬。這個(gè)類也描述了對(duì)象的行為:對(duì)象能夠響應(yīng)的selector以及其實(shí)現(xiàn)的實(shí)例方法。
The class's method list is the set of instance methods, the selectors that the object responds to. When you send a message to an instance,objc_msgSend()
looks through the method list of that object's class (and superclasses, if any) to decide what method to call.
類的方法列表是實(shí)例方法(也就是對(duì)象能夠響應(yīng)的selector)的集合瘫想。當(dāng)你給你一個(gè)實(shí)例發(fā)送消息時(shí)仗阅,objc_msgSend()
查找對(duì)象的類(以及超類,如果有超類的話)的方法列表国夜,以決定調(diào)用哪個(gè)方法减噪。
Each Objective-C class is also an object. It has anisa
pointer and other data, and can respond to selectors. When you call a "class method" like[NSObject alloc]
, you are actually sending a message to that class object.
每一個(gè)Objective-C類也是一個(gè)對(duì)象。它有一個(gè)isa
指針以及其他的一些數(shù)據(jù)车吹,可以響應(yīng)selector筹裕。當(dāng)你調(diào)用一個(gè)“類方法”時(shí),比如[NSObject alloc]
窄驹,你實(shí)際上是在向類對(duì)象發(fā)送消息朝卒。
Since a class is an object, it must be an instance of some other class: a metaclass. The metaclass is the description of the class object, just like the class is the description of ordinary instances. In particular, the metaclass's method list is the class methods: the selectors that the class object responds to. When you send a message to a class - an instance of a metaclass -objc_msgSend()
looks through the method list of the metaclass (and its superclasses, if any) to decide what method to call. Class methods are described by the metaclass on behalf of the class object, just like instance methods are described by the class on behalf of the instance objects.
既然類是一個(gè)對(duì)象,那么它一定是某個(gè)其他類的實(shí)例:也就是元類(metaclass)乐埠。元類是類對(duì)象的一種描述扎运,正如:類是普通實(shí)例的描述。需要指明的是饮戳,元類的方法列表是類方法,也就是類能夠響應(yīng)的selector洞拨。當(dāng)你向一個(gè)類(元類的實(shí)例)發(fā)送消息時(shí)扯罐,objc_msgSend()
查找元類(以及元類的超類,如果元類有超類的話)的方法列表以決定執(zhí)行哪一個(gè)方法烦衣。類方法是元類代表類對(duì)象所描述的歹河,正如:實(shí)例方法是類代表對(duì)象所描述的掩浙。
What about the metaclass? Is it metaclasses all the way down? No. A metaclass is an instance of the root class's metaclass; the root metaclass is itself an instance of the root metaclass. Theisa
chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclassisa
pointers rarely matters, since in the real world nobody sends messages to metaclass objects.
那元類呢?是不是元類還有元類秸歧,然后還有元類這樣下去厨姚?并不是的。元類是一個(gè)根類的元類的實(shí)例键菱。isa
鏈在此處成環(huán)結(jié)束:實(shí)例指向類指向元類指向根元類指向自己(根元類)谬墙。元類isa
指針的實(shí)際行為不太重要,因?yàn)閷?shí)際操作中不會(huì)有人向元類對(duì)象發(fā)送消息经备。
More important is the superclass of a metaclass. The metaclass's superclass chain parallels the class's superclass chain, so class methods are inherited in parallel with instance methods. And the root metaclass's superclass is the root class, so each class object responds to the root class's instance methods. In the end, a class object is an instance of (a subclass of) the root class, just like any other object.
元類的超類更為重要拭抬。元類的超類鏈和類的超類鏈?zhǔn)瞧叫械模灶惙椒ǖ睦^承關(guān)系和實(shí)例方法的繼承關(guān)系也是平行的侵蒙。根元類的超類就是根類造虎,所以每個(gè)類對(duì)象都響應(yīng)根類的實(shí)例方法。最后纷闺,類對(duì)象是根類的實(shí)例(或者說(shuō)子類)算凿,就和其他對(duì)象一樣。
Confused? The diagram may help. Remember, when a message is sent to any object, the method lookup starts with that object'sisa
pointer, then continues up the superclass chain. "Instance methods" are defined by the class, and "class methods" are defined by the metaclass plus the root (non-meta) class.
迷糊了嗎犁功?氓轰??波桩?上面那個(gè)圖應(yīng)該能幫你理解戒努。要記住的是,當(dāng)一個(gè)消息發(fā)送給任何一個(gè)對(duì)象镐躲,方法查找都從這個(gè)對(duì)象的isa
指針開(kāi)始储玫,然后沿著超類鏈繼續(xù)下去∮┰恚“實(shí)例方法”是類定義的撒穷,“類方法”是元類和根類(注意不是根元類)定義的。
In proper computer science language theory, a class and metaclass hierarchy can be more free-form, with deeper metaclass chains and multiple classes instantiated from any single metaclass. Objective-C uses metaclasses for practical goals like class methods, but otherwise tends to hide metaclasses. For example,[NSObject class]
is identical to[NSObject self]
, even though in formal terms it ought to return the metaclass thatNSObject->isa
points to. The Objective-C language is a set of practical compromises; here it limits the class schema before it gets too, well, meta.
用計(jì)算機(jī)科學(xué)語(yǔ)言的理論來(lái)講裆熙,類和元類的層級(jí)關(guān)系可以更自由端礼,有更深層次的元類鏈,有從某一個(gè)元類實(shí)例化出來(lái)的多個(gè)類入录。Objective-C使用元類是為了達(dá)到更實(shí)際的目的蛤奥,比如類方法,卻又希望隱藏元類(的概念)僚稿。比如說(shuō)凡桥,[NSObject class]
和[NSObject self]
其實(shí)是一模一樣的,盡管用更正式的術(shù)語(yǔ)來(lái)說(shuō)蚀同,[NSObject class]
應(yīng)該返回NSObject->isa
所指向的元類缅刽。Objective-C是為了實(shí)踐所折中的結(jié)果啊掏,它為了不變得過(guò)于meta而限制了類的schema。