博客
关于我
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 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>