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

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

一 概述

类在Objective-C中是一个非常重要的概念。类可以看作是一个对象,它本身也是一个Class类型的对象,简称为“类对象”。在Objective-C中,Class类型的定义是typedef struct objc_clas *Class。通过类名,我们可以引用对应的类对象,而每个类都有且仅有一个类对象。

二 类间关系

在Objective-C中,类之间可以有多种关系,包括继承和分类关系。

2.1 Person类

Person类是一个常见的例子,它继承自NSObject类。以下是Person类的接口定义:

// Person.h#import 
@interface Person : NSObject@property int age;+(void)test;@end

Person类的实现代码如下:

// 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类继承自Person类,并添加了一些特定的属性和方法。以下是Student类的接口定义:

// Student.h#import "Person.h"@interface Student : Person@end

Student类的实现代码如下:

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

2.3 GoogStudent类(Student的子类)

GoogStudent类继承自Student类,进一步扩展了Student类的功能。以下是GoogStudent类的接口定义:

// GoodStudent.h#import "Student.h"@interface GoodStudent : Student@end

GoogStudent类的实现代码如下:

// 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)类是Person类的一个分类,提供了额外的功能。以下是Person(MJ)类的接口定义:

// Person+MJ.h#import "Person.h"@interface Person (MJ)@end

Person(MJ)类的实现代码如下:

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

三 类的本质

在Objective-C中,类是程序中的基本构建块。以下是关于类本质的详细说明。

3.1 Class

在Objective-C中,Class类型的定义是typedef struct objc_clas *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)

类在初始化时会执行+load+initialize方法。+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

在Objective-C中,+load+initialize是类方法,用于初始化类的行为。

4.1 +load

+load方法会在程序启动时自动调用,并且遵循以下顺序执行:

  • 先加载父类,再加载子类。
  • 先加载元原始类,再加载分类。
  • 即使类没有被使用,也会在程序启动时加载。
  • 4.2 +initialize

    +initialize方法是在第一次使用类时调用一次,顺序也是先调用父类的+initialize,再调用子类的+initialize

    4.3 获取类对象的2种方式

  • 类方法方式:Class c = [Person class];
  • 对象方法方式:Person *p = [Person new]; Class c2 = [p class];
  • 通过以上方法,可以轻松获取类对象。

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

    你可能感兴趣的文章
    Objective-C实现Dinic算法(附完整源码)
    查看>>
    Objective-C实现disjoint set不相交集算法(附完整源码)
    查看>>
    Objective-C实现DisjointSet并查集的算法(附完整源码)
    查看>>
    Objective-C实现djb2哈希算法(附完整源码)
    查看>>
    Objective-C实现DNF排序算法(附完整源码)
    查看>>
    Objective-C实现doomsday末日算法(附完整源码)
    查看>>
    Objective-C实现double factorial iterative双阶乘迭代算法(附完整源码)
    查看>>
    Objective-C实现double factorial recursive双阶乘递归算法(附完整源码)
    查看>>
    Objective-C实现double hash双哈希算法(附完整源码)
    查看>>
    Objective-C实现double linear search recursion双线性搜索递归算法(附完整源码)
    查看>>
    Objective-C实现double linear search 双线性搜索算法(附完整源码)
    查看>>
    Objective-C实现double sort双重排序算法(附完整源码)
    查看>>
    Objective-C实现DoublyLinkedList双链表的算法(附完整源码)
    查看>>
    Objective-C实现DoublyLinkedList双链表算法(附完整源码)
    查看>>
    Objective-C实现DPLL(davisb putnamb logemannb loveland)算法(附完整源码)
    查看>>
    Objective-C实现DWT离散小波变换(附完整源码)
    查看>>
    Objective-C实现Edmonds-Karp算法(附完整源码)
    查看>>
    Objective-C实现EEMD算法(附完整源码)
    查看>>
    Objective-C实现elgamal 密钥生成器算法(附完整源码)
    查看>>
    Objective-C实现EM算法(附完整源码)
    查看>>