OC基础(12)

时间:2015-12-03 13:42:54   收藏:0   阅读:286

new方法实现原理

本小节知识:

  1. 【掌握】new方法实现原理

1.new方法实现原理

This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.

 

类的本质

本小节知识:

  1. 【了解】类的本质
  2. 【掌握】如何获取类对象
  3. 【理解】类对象的用法
  4. 【理解】类对象的存储
  5. 【了解】OC实例对象类对象元数据之间关系

1.类的本质


2.如何获取类对象

格式:[实例对象   class ];
如:   [dog class];
格式:[类名 class];
如:[Dog class]

3.类对象的用法

[Dog test];

Class c = [Dog class];
[c test];
Dog *g = [Dog new];

Class c = [Dog class];
Dog *g1 = [c new];

4.类对象的存储

技术分享


5.OC实例对象 类对象 元对象之间关系

NSObject.h
@interface NSObject <NSObject> {
    Class isa  OBJC_ISA_AVAILABILITY;
}
objc.h
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

/// Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};
runtime.h
struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;

 

类的启动过程

本小节知识:

  1. 【掌握】+load方法
  2. 【掌握】+initialize方法

1.+load方法

@implementation Person

+ (void)load
{
    NSLog(@"%s", __func__);
}
@end

@implementation Student : Person

+ (void)load
{
    NSLog(@"%s", __func__);
}
@end

输出结果:
+[Person load]
+[Student load]

2.+initialize

@implementation Person
+ (void)initialize
{
    NSLog(@"%s", __func__);
}
@end

@implementation Student : Person
+ (void)initialize
{
    NSLog(@"%s", __func__);
}
@end
int main(int argc, const char * argv[]) {
    Student *stu = [Student new];
    return 0;
}
输出结果:
+[Person initialize]
+[Student initialize]

原文:http://www.cnblogs.com/zhoudaquan/p/5015797.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!