类和元类 class and metaclass

本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号: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的对象系统。每个对象都是某个类的实例,对象的isa指针指向其类。那个类描述了对象的数据:占用空间大小、ivar类型以及样式。这个类也描述了对象的行为:对象能够响应的selector以及其实现的实例方法。
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)的集合。当你给你一个实例发送消息时,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指针以及其他的一些数据,可以响应selector。当你调用一个“类方法”时,比如[NSObject alloc],你实际上是在向类对象发送消息。
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。当你向一个类(元类的实例)发送消息时,objc_msgSend()查找元类(以及元类的超类,如果元类有超类的话)的方法列表以决定执行哪一个方法。类方法是元类代表类对象所描述的,正如:实例方法是类代表对象所描述的。
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链在此处成环结束:实例指向类指向元类指向根元类指向自己(根元类)。元类isa指针的实际行为不太重要,因为实际操作中不会有人向元类对象发送消息。
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.
迷糊了吗????上面那个图应该能帮你理解。要记住的是,当一个消息发送给任何一个对象,方法查找都从这个对象的isa指针开始,然后沿着超类链继续下去。“实例方法”是类定义的,“类方法”是元类和根类(注意不是根元类)定义的。
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。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,670评论 0 9
  • 翻译参考链接:[objc 解释]:类和元类 英文原文:[objc explain]: Classes and me...
    kakukeme阅读 973评论 0 51
  • Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的...
    有一种再见叫青春阅读 570评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,490评论 18 139
  • 可是他们不爱我并不是我的错,我完全可以坦然接受这个事实,然后过好我的生活。我为什么对父亲的行为感到无法接受...
    danaom阅读 228评论 0 0