这个示例有两部分功能:
1. 大家只看`Person`的演示功能即可。
2. 观察Person和Animal两个对象的打印(打印方法名的可以注释掉,将main方法中的代码注释打开)
`Person`的演示功能(不打印方法名称)
This object is 0x100408400.
Class is Person, and super is NSObject.
MetaClass is 0x100001328
Following the isa pointer 1 times gives 0x100001350
Following the isa pointer 2 times gives 0x100001328
Following the isa pointer 3 times gives 0x7fffb9a4f0f0
Following the isa pointer 4 times gives 0x7fffb9a4f0f0
NSObject's class is 0x7fffb9a4f140
NSObject's meta class is 0x7fffb9a4f0f0
我们来观察isa到达过的地址的值:
对象的地址是 0x100408400. 类的地址是 0x100001350. 元类的地址是 0x100001328. 根元类(NSObject的元类)的地址是 0x7fffb9a4f0f0.对于本次打印我们可以做出以下结论(可以再去看一遍上边那张精髓的图):
对于3、4次打印相同,就是因为NSObject元类的类是它本身. 我们在实例化对象的时候,其实是创建了许多对象,这就是我们说的类簇。也对应了我们在用runtime创建类的时候`objc_allocateClassPair(xx,xx)`中是`ClassPair`而不是`bjc_allocateClass` 通过地址的大小也可以看出对象实例化先后,地址越小的越先实例化 很好的诠释了上边那张精髓图isa的指向 NSObject的两个地址都非常大(哈哈哈哈哈!为什么非常大啊??接下往下看)`Person`的演示功能(打印方法名称)
Class is Person, and super is NSObject.
MetaClass is 0x100002378
Following the isa pointer 1 times gives 0x1000023a0
---------------**1 start**-----------------------
sel------printMethod:methods:
sel------print
---------------**1 end**-----------------------
Following the isa pointer 2 times gives 0x100002378
---------------**2 start**-----------------------
sel------printStatic
---------------**2 end**-----------------------
Following the isa pointer 3 times gives 0x7fffb9a4f0f0
---------------**3 start**-----------------------
我只把重要的复制出来了,`NSObject`的所有的方法名没有复制出来,在此处不是重要的。
此次打印结果的结论:
类方法(静态方法)是存储在元类中的
观察Person和Animal两个对象的打印
This object is 0x100508e70.
Class is Person, and super is NSObject.
MetaClass is 0x100001338
Following the isa pointer 1 times gives 0x100001360
Following the isa pointer 2 times gives 0x100001338
Following the isa pointer 3 times gives 0x7fffb9a4f0f0
Following the isa pointer 4 times gives 0x7fffb9a4f0f0
NSObject's class is 0x7fffb9a4f140
NSObject's meta class is 0x7fffb9a4f0f0
--------------------------------
This object is 0x100675ed0.
Class is Animal, and super is NSObject.
MetaClass is 0x100001388
Following the isa pointer 1 times gives 0x1000013b0
Following the isa pointer 2 times gives 0x100001388
Following the isa pointer 3 times gives 0x7fffb9a4f0f0
Following the isa pointer 4 times gives 0x7fffb9a4f0f0
NSObject's class is 0x7fffb9a4f140
NSObject's meta class is 0x7fffb9a4f0f0
Program ended with exit code: 0










