博客
关于我
OC开发之——类的本质(33)
阅读量:81 次
发布时间:2019-02-26

本文共 1905 字,大约阅读时间需要 6 分钟。

一 概述

  • 类也是一个对象,是Class类型的对象,简称“类对象”
  • Class类型的定义 typedef struct objc_clas *Class
  • 类名就代表着类对象,每个类只有一个类对象

二 类间关系

2.1 Person类

//Person.h#import 
@interface Person : NSObject@property int age;+(void)test;@end//Person.m#import "Person.h"@implementation Person+(void)test{ NSLog(@"Person调用了test++++++");}+ (void)load{ NSLog(@"Person----load");}+(void)initialize{ NSLog(@"Person---initialize");}@end

2.2 Student类(Person的子类)

//Student.h#import "Person.h"@interface Student : Person@end//Student.m#import "Student.h"@implementation Student+(void)load{    NSLog(@"Student---load");}+(void)initialize{    NSLog(@"Student---initialize");}@end

2.3 GoogStudent类(Student的子类)

//GoodStudent.h#import "Student.h"@interface GoodStudent : Student@end//GoodStudent.m#import "GoodStudent.h"@implementation GoodStudent+(void)load{    NSLog(@"GoodStudent---load");}+(void)initialize{    NSLog(@"GoodStudent---initialize");}@end

2.4 Person(MJ)类(Person的分类)

//Person+MJ.h#import "Person.h"@interface Person (MJ)@end//Person+MJ.m#import "Person+MJ.h"@implementation Person (MJ)+(void)load{ NSLog(@"Person(MJ)---load");}+(void)initialize{    NSLog(@"Person(MJ)---initialize");}@end

三 类的本质

3.1 Class

Person *p1=[[Person alloc]init]; Person *p2=[[Person alloc]init]; Person *p3=[[Person alloc]init];          Class c1=[p1 class]; Class c2=[p2 class]; Class c3=[Person class]; NSLog(@"c1=%p,c2=%p,c3=%p",c1,c2,c3);

3.2 类的初始化(load,initialize)

Person *p=[[Person alloc]init];Class c=[p class];       [Person test];[c test];        Person *p2=[[c new]init]; NSLog(@"Person ---%d",p2.age);

四 +load和+initialize

4.1 +load

  • 在程序启动时就会加载所有的类和分类,并调用所有类和分类的+load方法
  • 先加载父类,再加载子类:也就是先调用父类的+load,再调用子类的+load
  • 先加载元原始类,再加载分类
  • 不管程序运行过程中有木有用到这个类,都会调用+load加载

4.2 +initialize

  • 在第一次使用某个类时(比如创建对象等)就会调用一次+initialize方法
  • 一个类只会调用一次+initialize方法,先调用父类的,再调用子类的

4.3 获取类对象的2种方式

//方式一Class c=[Person class];//类方法方式二Person *p=[Person new];Class c2=[p class];//对象方法

转载地址:http://jjik.baihongyu.com/

你可能感兴趣的文章
MySQL索引介绍及百万数据SQL优化实践总结
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>
MySQL索引和查询优化
查看>>
mysql索引底层数据结构和算法
查看>>
Mysql索引底层结构的分析
查看>>
MySQL索引底层:B+树详解
查看>>
Mysql索引总结
查看>>
mysql索引最左匹配原则理解以及常见的sql使用的索引情况的实测
查看>>
Mysql索引类型
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
mysql索引能重复吗_mysql “索引”能重复吗?“唯一索引”与“索引”区别是什么?...
查看>>
MySQL索引详解(IT枫斗者)
查看>>
MySQL索引那些事:什么是索引?为什么加索引就查得快了?
查看>>
Mysql索引(1):索引概述
查看>>
Mysql索引(2):索引结构
查看>>