Classes and metaclasses
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html
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.
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.
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.
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.
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.
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.
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->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.
Classes and metaclasses的更多相关文章
- Python高级特性(3): Classes和Metaclasses(转)
原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变 ...
- Understanding Python metaclasses
转载:https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ None of the existing article ...
- iOS运行时 -- Runtime(摘抄自网络)
运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个 ...
- 运行时(iOS)
运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得 ...
- iOS 动态特性和RunTime
过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语 ...
- 理解Python元类(转)
add by zhj:先收藏了,有时间看,图倒是不少,可以配合stackover flow上那篇文章一起看 原文:http://blog.ionelmc.ro/2015/02/09/understan ...
- ios Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语 ...
- Objective-C的动态特性
最近几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语言 ...
- RunTime 入门
原文链接:http://www.jianshu.com/p/59992507f875 这是一篇浅显实用 易记 易理解的关于runtime的解读. Runtime 中的方法主要以五个单词开头——clas ...
随机推荐
- 【原】spring+springmvc+mybatis整合
整合框架的代码结构: 最全约束: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- 教妹学 Java:晦涩难懂的泛型
00.故事的起源 “二哥,要不我上大学的时候也学习编程吧?”有一天,三妹突发奇想地问我. “你确定要做一名程序媛吗?” “我觉得女生做程序员,有着天大的优势,尤其是我这种长相甜美的.”三妹开始认真了起 ...
- 当我们聊kubernetes operator时,我们在聊些什么
不聊什么 在开始聊operator前,先说说这篇文章里我们不聊什么.我们这里不聊operator的具体实现,不聊operator的由来历史,不聊operator的hello world.如果想了解这些 ...
- 浅析Jupyter Notebook
一 概述 Jupyter Notebook是以web交互式的编程接口,是IPython notebook的升级版本.主要是针对python,另外支持运行 40 多种编程语言.Jupyter可以在个人机 ...
- Vue培训项目总结
昨天是最后一次给同事进行Vue的培训,这次培训主要是以基础入门为主. 整篇讲义参考了一些文章:https://gitbook.cn/gitchat/column/5a4af1c5658b7c0d9eb ...
- 基于SpringBoot构建分模块项目
前言 步骤过于详细,多图慎入!!! 假设一个场景,要开发一个4s店维修部的办公系统,其功能有:前台接待,维修抢单,财务结算,库存管理.于是我们创建一个项目balabalabala写完交工. 一段时间后 ...
- JS高级学习历程-4
4 执行环境可以访问什么变量 具体可以访问变量类型:局部变量.参数.函数.外部环境变量 优先级:局部变量 > 函数 > 参数 > 外部环境变量 <!DOCTYPE html&g ...
- SWT的基本组件使用
1按钮组件(Button) (1)Button组件常用样式 SWT.PUSH按钮 SWT.CHECK多选按钮 SWT.RADIO单选按钮 SWT.ARROW箭头按钮 SWT.NONE默认按钮 SWT. ...
- 字典排序permutation
理论 C++ 中的next_permutation 一般作为正序全排列的使用规则,其实这个就是正序字典排序的实现. 比如我们要对 列表 [1,2,3] 做full permutation 一般使用递 ...
- BZOJ 1116: [POI2008]CLO 并查集
成立时当且仅当每个联通块都有环存在.一个连通块若有m个点,则必有多于m条有向边,可用并查集来维护. #include<cstdio> #include<iostream> #d ...