zoukankan      html  css  js  c++  java
  • Object-C——核心语法(2)

    一、分类(Category)

        1、分类的作用:在不改变原来类内容的基础上,可以为类增加一些方法

        2、格式

             ①  分类的声明        

    1 @interface 类名 (分类名称)
    2 // 方法声明
    3 @end

            ②   分类的实现

    1 @implementation 类名 (分类名称)
    2 // 方法实现
    3 @end

        3、 使用注意:
         ①  分类只能增加方法,不能增加成员变量
         ②  分类方法实现中可以访问原来类中声明的成员变量
         ③  分类可以重新实现原来类中的方法,但是会覆盖掉原来的方法,会导致原来的方法没法再使用
         ④  方法调用的优先级:分类(最后参与编译的分类优先) --> 原来类  --> 父类

       

     1 //代码举例
     2 #import <Foundation/Foundation.h>
     3 #import "Person.h"
     4 #import "Person+MJ.h"
     5 #import "Person+JJ.h"
     6 
     7 int main()
     8 {
     9     Person *p = [[Person alloc] init];
    10    
    11     
    12     // 优先去分类中查找,然后再去原来类中找,最后再去父类中找
    13     [p test];
    14   
    15     
    16     return 0;
    17 }

      4、给系统自带的类NSString添加分类
           ①  给NSString增加一个类方法:计算某个字符串中的阿拉伯数字的个数

     1 //.h文件
     2 #import <Foundation/Foundation.h>
     3 
     4 @interface NSString (Number)
     5 
     6 + (int)numberCountOfString:(NSString *)str;
     7 
     8 @end
     9 //.m文件
    10 #import "NSString+Number.h"
    11 
    12 @implementation NSString (Number)
    13 
    14 + (int)numberCountOfString:(NSString *)str
    15 {
    16     1.定义变量计算数字的个数
    17   int count = 0;
    18    
    19     for (int i = 0; i<str.length; i++)
    20    {
    21         unichar c = [str characterAtIndex:i];
    22         
    23         if ( c>='0' && c<='9')
    24         {
    25            count++;
    26    }
    27     return count;
    28     
    29     return [str numberCount];
    30 }
    31 @end

           ②  给NSString增加一个对象方法:计算当前字符串中的阿拉伯数字的个数

     1 //.h文件
     2 #import <Foundation/Foundation.h>
     3 
     4 @interface NSString (Number)
     5 
     6 - (int)numberCount;
     7 
     8 @end
     9 //.m文件
    10 #import "NSString+Number.h"
    11 
    12 @implementation NSString (Number)
    13 - (int)numberCount
    14 {
    15     int count = 0;
    16     
    17     for (int i = 0; i<self.length; i++)
    18     {
    19         // 取出i这个位置对应的字符
    20         unichar c = [self characterAtIndex:i];
    21         
    22         // 如果这个字符是阿拉伯数字
    23         if ( c>='0' && c<='9' )
    24         {
    25             count++;
    26         }
    27     }
    28     
    29     return count;
    30 }
    31 
    32 @end

    三、类的深入研究
       1、 类的本质

             ①  类也是对象,是Class类型的对象,简称“类对象”

             ②  类名代表类对象,每个类只有一个类对象

       2、+load和+initialize

            ①  +load

             * 当程序启动时,就会加载项目中所有的类和分类,而且加载后会调用每个类和分类的+load方法。只会调用一次

             *先加载父类,再加载子类(先调用父类的+load方法,再调用子类的+load方法)

     1 #import "Student.h"
     2 
     3 @implementation Student
     4 
     5 // 在类被加载的时候调用
     6 + (void)load
     7 {
     8     NSLog(@"Student---load");
     9 }
    10 
    11 
    12 + (void)initialize
    13 {
    14     NSLog(@"Student-initialize");
    15 }
    16 
    17 
    18 @end

             ②  +initialize

              *当第一次使用某个类时,就会调用当前类的+initialize方法

              *先初始化父类,再初始化子类(先调用父类的+initialize方法,再调用子类的+initialize方法)

              ③程序举例

     1 #import "Person.h"
     2 
     3 @implementation Person
     4 + (void)test
     5 {
     6     NSLog(@"调用了test方法");
     7 }
     8 
     9 
    10 // 当程序启动的时候,就会加载一次项目中所有的类。类加载完毕后就会调用+load方法
    11 + (void)load
    12 {
    13     NSLog(@"Person---load");
    14 }
    15 
    16 // 当第一次使用这个类的时候,就会调用一次+initialize方法
    17 + (void)initialize
    18 {
    19     NSLog(@"Person-initialize");
    20 }
    21 
    22 @end

         3、获取类对象的方法

               ①获取内存中的类对象

    Class c = [Person class];

               ②获取内存中的类对象

    Person *p = [Person new];
    Class c2 = [p class];

       4、 类对象调用类方法

    1 Class c = [Person class];
    2 Person *p2 = [c new];

    四、 description方法

       1、-description方法

         使用NSLog和%@输出某个对象时,会调用对象的-description方法,并拿到返回值进行输出

     1 #import "Person.h"
     2 
     3 @implementation Person
     4 
     5  //决定了实例对象的输出结果
     6 - (NSString *)description
     7 {
     8    
     9     return [NSString stringWithFormat:@"age=%d, name=%@", _age, _name];
    10    
    11 }
    12 @end
     1 void test1()
     2 {
     3     Person *p = [[Person alloc] init];
     4     p.age = 20;
     5     p.name = @"Jack";
     6     // 默认情况下,利用NSLog和%@输出对象时,结果是:<类名:内存地址>
     7     
     8     // 1.会调用对象p的-description方法
     9     // 2.拿到-description方法的返回值(NSString *)显示到屏幕上
    10     // 3.-description方法默认返回的是“类名+内存地址”
    11     NSLog(@"%@", p);
    12   }  
    13   

       2、 +description方法

         使用NSLog和%@输出某个对象时,会调用对象的+description方法,并拿到返回值进行输出

     1 #import "Person.h"
     2 
     3 @implementation Person
     4 // 决定了类对象的输出结果
     5 + (NSString *)description
     6 {
     7     return @"Abc";
     8 }
     9 
    10 @end
    1 void test2()
    2 {
    3     Class c = [Person class];
    4     
    5     // 1.会调用类的+description方法
    6     // 2.拿到+description方法的返回值(NSString *)显示到屏幕上
    7     NSLog(@"%@", c);
    8 }

       3、 修改NSLog的默认输出
           重写-description或者+description方法

       4、注意事项

    1 // 下面代码会引发死循环
    2 //    // NSLog(@"%@", self);

    五、 SEL方法

         SEL其实是对方法的一种包装,将方法包装成一个SEL类型的数据,去找对应的方法地址。找到方法地址就可以调用方法,其实消息就是SEL

         *每个方法都有一个与之对应的SEL类型的对象

         *根据一个SEL对象就可以找到方法的地址,进而调用方法

          1、 SEL类型的定义

    typedef struct objc_selector     *SEL;

          2、SEL对象的创建
            

    1 SEL s = @selector(test);
    2 SEL s2 = NSSelectorFromString(@"test");

          3、 SEL对象的用法
               ①  把test2包装成SEL类型的数据
               ②  根据SEL数据找到对应的方法地址
               ③  根据方法地址调用对应的方法

    1  NSString *name = @"test2";
    2    
    3     SEL s = NSSelectorFromString(name);
    4    
    5     [p performSelector:s];
    6     
    7     [p performSelector:@selector(test2)];
    8     [p test2];

      

        

  • 相关阅读:
    【开源】使用Angular9和TypeScript开发RPG游戏
    [开源]开放域实体抽取泛用工具 NetCore2.1
    【开源】C#信息抽取系统【招募C#队友】
    自然语言处理真实项目实战(20170830)
    自然语言处理真实项目实战(20170822)
    智慧航空AI大赛-阿里云算法大赛总结 第一赛季总结
    iOS11和机器学习CoreML库
    [不定期更新长文]卷积神经网络工作原理研究
    微服务SpringCloud之GateWay熔断、限流、重试
    微服务SpringCloud之GateWay服务化和过滤器
  • 原文地址:https://www.cnblogs.com/gaizuojia/p/4357468.html
Copyright © 2011-2022 走看看