博客
关于我
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/

你可能感兴趣的文章
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>