類和元類 class and metaclass

本文為大地瓜原創(chuàng),歡迎知識共享,轉載請注明出處。
雖然你不注明出處我也沒什么精力和你計較。
作者微信號:christgreenlaw


本文的原文。本文只對其進行翻譯。


實例、類、isa、元類、子類、超類、根類等概念關系

Objective-C is a class-based object system. Each object is an instance of some class; the object's isa 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是一個基于class的對象系統(tǒng)。每個對象都是某個類的實例,對象的isa指針指向其類。那個類描述了對象的數(shù)據:占用空間大小、ivar類型以及樣式。這個類也描述了對象的行為:對象能夠響應的selector以及其實現(xiàn)的實例方法。
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.
類的方法列表是實例方法(也就是對象能夠響應的selector)的集合。當你給你一個實例發(fā)送消息時,objc_msgSend()查找對象的類(以及超類,如果有超類的話)的方法列表,以決定調用哪個方法。
Each Objective-C class is also an object. It has an isa 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.
每一個Objective-C類也是一個對象。它有一個isa指針以及其他的一些數(shù)據,可以響應selector。當你調用一個“類方法”時,比如[NSObject alloc],你實際上是在向類對象發(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.
既然類是一個對象,那么它一定是某個其他類的實例:也就是元類(metaclass)。元類是類對象的一種描述,正如:類是普通實例的描述。需要指明的是,元類的方法列表是類方法,也就是類能夠響應的selector。當你向一個類(元類的實例)發(fā)送消息時,objc_msgSend()查找元類(以及元類的超類,如果元類有超類的話)的方法列表以決定執(zhí)行哪一個方法。類方法是元類代表類對象所描述的,正如:實例方法是類代表對象所描述的。
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. The isa chain ends in a cycle here: instance to class to metaclass to root metaclass to itself. The behavior of metaclass isa pointers rarely matters, since in the real world nobody sends messages to metaclass objects.
那元類呢?是不是元類還有元類,然后還有元類這樣下去?并不是的。元類是一個根類的元類的實例。isa鏈在此處成環(huán)結束:實例指向類指向元類指向根元類指向自己(根元類)。元類isa指針的實際行為不太重要,因為實際操作中不會有人向元類對象發(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.
元類的超類更為重要。元類的超類鏈和類的超類鏈是平行的,所以類方法的繼承關系和實例方法的繼承關系也是平行的。根元類的超類就是根類,所以每個類對象都響應根類的實例方法。最后,類對象是根類的實例(或者說子類),就和其他對象一樣。
Confused? The diagram may help. Remember, when a message is sent to any object, the method lookup starts with that object's isa 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.
迷糊了嗎????上面那個圖應該能幫你理解。要記住的是,當一個消息發(fā)送給任何一個對象,方法查找都從這個對象的isa指針開始,然后沿著超類鏈繼續(xù)下去。“實例方法”是類定義的,“類方法”是元類和根類(注意不是根元類)定義的。
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 that NSObject->isapoints to. The Objective-C language is a set of practical compromises; here it limits the class schema before it gets too, well, meta.
用計算機科學語言的理論來講,類和元類的層級關系可以更自由,有更深層次的元類鏈,有從某一個元類實例化出來的多個類。Objective-C使用元類是為了達到更實際的目的,比如類方法,卻又希望隱藏元類(的概念)。比如說,[NSObject class][NSObject self]其實是一模一樣的,盡管用更正式的術語來說,[NSObject class]應該返回NSObject->isa所指向的元類。Objective-C是為了實踐所折中的結果,它為了不變得過于meta而限制了類的schema。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 轉至元數(shù)據結尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉至元數(shù)據起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評論 0 9
  • 翻譯參考鏈接:[objc 解釋]:類和元類 英文原文:[objc explain]: Classes and me...
    kakukeme閱讀 1,111評論 0 51
  • Objective-C語言是一門動態(tài)語言,它將很多靜態(tài)語言在編譯和鏈接時期做的事放到了運行時來處理。這種動態(tài)語言的...
    有一種再見叫青春閱讀 669評論 0 3
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • 可是他們不愛我并不是我的錯,我完全可以坦然接受這個事實,然后過好我的生活。我為什么對父親的行為感到無法接受...
    danaom閱讀 255評論 0 0

友情鏈接更多精彩內容