OC语法基础(8)-- OC中特有的…

categoary分类

//  Person.h

#import

 

@interface Person : NSObject

{

    @private

    int _age;

}

 

@property (nonatomic,assign) int age;

@property (nonatomic,strong) NSString * name;

 

//人类的基本行为

//吃喝跑跳哭笑

- (void)eat;

- (void)drink;

- (void)run;

- (void)jump;

- (void)cry;

- (void)smile;

 

 

 

//恐怖分子

 

 

 

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)eat

{

    NSLog(@"吃东西");

}

- (void)drink

{

    NSLog(@"喝东西");

}

- (void)run

{

    NSLog(@"跑起来了");

}

- (void)jump

{

        NSLog(@"跳一跳");

}

- (void)cry

{

    NSLog(@"哭一个");

}

- (void)smile

{

    NSLog(@"给大爷笑一个");

}

@end

//  Person+BlackPerson.h

#import "Person.h"

 

@interface Person (BlackPerson)

//不能在分类中生成员变量

//{

//    int _weight;

//}

//黑人技能

- (void)superRun;

- (void)superJump;

- (void)eat;

@end

//  Person+BlackPerson.m

#import "Person+BlackPerson.h"

 

@implementation Person (BlackPerson)

 

//黑人技能

- (void)superRun

{

    NSLog(@"超级跑");

}

- (void)superJump

{

//    [self eat];

    [self setAge:10];

    _age = 20;//只要是在原类中声明的成员变量都可以在分类中直接访问

                  // .h中显示声明的成员变量_age可以在分类中直接访问,而用property生成的是不可以访问的

    NSLog(@"超级跳");

}

// 如果分类中定义实现了与原类中相同的方法,那么原类中的方法相当于被覆盖掉了

// 在main函数中引入多个类扩展,而每个类扩展又都覆盖了原来的方法,那么在main函数中调用被覆盖的方法到底是

// 哪个方法这取决于文件的编译顺序,这就出现了不确定因素所有

// 在实际的开发中,最好不要出现方法覆盖

- (void)eat

{

 

    NSLog(@"吃货表现");

}

 

@end

//  Person+Kongbufenzi.h

#import "Person.h"

 

@interface Person (Kongbufenzi)

- (void)selfBoom;

- (void)bigBoom;

- (void)eat;

@end

//  Person+Kongbufenzi.m

#import "Person+Kongbufenzi.h"

 

@implementation Person (Kongbufenzi)

- (void)selfBoom

{

    NSLog(@"自爆卡车");

}

- (void)bigBoom

{

    NSLog(@"尤里复仇哦");

}

- (void)eat

{

    NSLog(@"恐怖分子吃东西");

}

@end

//  main.m

#import

 

#import "Person.h"

#import "Person+BlackPerson.h"

#import "Person+Kongbufenzi.h"

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

       

        Person * p = [[Person alloc] init];

       

        [p eat];

       

        [p superJump];

       

        [p selfBoom];

       

       

    }

    return 0;

}

 

分类应用<<span style="font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-fareast-font-family: 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:"Times New Roman"">多用于扩展系统提供的类>

//  main.m

#import

 

#import "NSString+NSStringExtend.h"

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

       

//        NSString * str = @"ab1d3,5,5,4";

//       

//        int count = [NSString numberCountOfString:str];

//        NSLog(@"%d",count);

 

//        NSString * str = @"adf378";//字符串对象

//       

//       

//        NSLog(@"new %d",[@"adf378" numberOfCount]);

       

       

        int count = [NSString numberCountOfString:@"12345"];

        NSLog(@"%d",count);

    }

    return 0;

}

//  NSString+NSStringExtend.h

#import

 

@interface NSString (NSStringExtend)

 

+ (int)numberCountOfString:(NSString *)str;

- (int)numberOfCount;

 

@end

//  NSString+NSStringExtend.m

#import "NSString+NSStringExtend.h"

 

@implementation NSString (NSStringExtend)

//ab1c890

+ (int)numberCountOfString:(NSString *)str

{

   

   

    return [str numberOfCount];

}

 

- (int)numberOfCount

{

    int count = 0;

    //1.把每一个字符拿出来比较

    for (int i = 0; i < self.length; i++)

    {

        unichar c = [self characterAtIndex:i];

       

        if (c >= '0' && c <= '9' )

        {

            count++;

        }

       

    }

    return count;

}

 

@end

block数据类型

//  Calculate.h

#import

 

typedef int (^calculateBlock)(int a,int b);

 

@interface Calculate : NSObject

 

- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate;

 

@end

//  Calculate.m

#import "Calculate.h"

 

@implementation Calculate

 

 

 

 

- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate

{

    //经常变化的功能,在设计当中叫做封装变化

    return calculate(number1,number2);

}

 

@end

//  main.m

// 本文件旨在说明block:

// 可以把它理解成一个匿名函数,但是这种匿名函数是一个block类型的,它可以作为参数传递

#import

#import "Calculate.h"

//数据类型的作用

//1.作为参数传递

//2.作为函数的返回值

//3.声明成变量

void test()

{

    NSLog(@"test");

}

int sum(int a, int b)

{

    return a + b;

}

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

       

        //int 4 float double 8 char

        //更加合理的分配内存空间

        int ca =10;

// OC中新增加下面几个类型

        //对象类型 NSObject * obj

        //id

        //BOOL

        //block 指向函数的指针比较像

        //SEL

       

       

       

        //block就是弥补了 指向函数的指针,不能够直接保存一个函数体(代码块)的缺陷

       

       

        //如果想要改变,block代码块之外的变量值,就必须在变量前加入<</span>实际使用时很少改变外部的变量>

        //__block关键字

        __block int x = 0;

       

        int (^sumBlock)(int a, int b) = ^int (int a, int b) {

           

            int result = (a * b);

            x = result;

            return result;

        };

       

        //当在开发中,你发现一个方法中的算法,可以有多种实现,你一时还不能确定用哪种更好,你就可以吧方法中// 其中一个参数定义成block方式<</span>比如下面的sumBlock,在其实现中你可以随意定义sumBlock的具体运算内容

// 其内容可以是加减陈处或其它运算,这样在需求发生变化时,更容易维护修改>

        //

       

        Calculate * cal = [[Calculate alloc] init];

       

        int sum = [cal calculateWithNumber1:10 andNumber2:20 andCalculate:sumBlock];

       

        NSLog(@"sum = %d",sum);

       

        NSLog(@"x = %d",x);

    }

    return 0;

}

补充点:指向函数的指针

//  main.c

// 指向函数的指针的定义初始化、引用、函数指针做函数参数

#include

 

//函数名就是函数的地址,  数组名就是数组的地址

void test()

{

    printf("老婆我们家我做主\n");

}

 

int sum(int val1, int val2)

{

    return val1 + val2;

}

 

int demo(int (*p)(int val1, int val2))   //int (*p)(int val1, int val2) = sum;

{

    int result = 0;

    if (0) {

        result = p(30, 50);

    }else{

        result = -1;

    }

    return result;

}

 

int main(int argc, const char * argv[])

{

   

//    block

 

//    函数指针常用来把函数当作参数来传递,用于接收某个函数

    int result = demo(sum);

    printf("result = %d\n", result);

    return 0;

}

 

 

void test3()

{

   

   

    int (*p)(int val1, int val2);

    p = sum;

   

    //    int result  = sum(10, 20);

    //    int result  = (*p)(20, 20);

    //    int result = p(30, 50);

    //    printf("result = %d\n", result);

}

 

void test2()

{

    //    int a = 10;

    //    int *p = &a;

    //

    //    *p = 33;

    //    printf("%d\n", *p);

    //    test();

   

   

   

    //    定义了一个指向函数的指针,这个指针将来指向的函数没有返回值没有形参

    void (*pointer)();

    pointer = test;//注意函数名称后面千万不要写括号

    //    *pointer == test // 这里只是一块地址 要想间接访问函数,如下行所示

    //    (*pointer)(); //间接调用

    //    test(); // 直接调用

    pointer(); //间接调用

   

    //    int ages[2] = {1, 3};

    //    int *p = ages;

    //

    //    printf("%d", ages[0]);

    //    printf("%d", p[0]);

    //    printf("%d", *(p + 0));

   

}

 

指向函数的指针练习

#include

 

void upper(char *p);

void change(char *str, void (*up)(char*));

int main(int argc, const char * argv[])

{

   

//    1.提示用户输入一句英文,单词之间用空格隔开

    printf("输入一句英文,单词之间用空格隔开\n");

//    2.接收用户输入的英文

//    char *container;

    char container[100];

    gets(container);

//    3.转换句子

   

//    char c = 'a';

//    upper(&c);

//    printf("c = %c\n", c);

   

//    char str[100] = " lnj cool";

    printf("转换前:%s\n", container);

//    change(str);

    change(container, upper);

    printf("转换后:%s\n", container);

   

    return 0;

}

 

void upper(char *p)

{

//    1.取出p指向存储空间的值

    char temp = *p;

//    2.转换为大写

    if (temp >= 'a' && temp <= 'z') {

        temp = temp - ('a' - 'A');

    }

//    3.赋值会指向的存储空间

    *p = temp;

}

 

// C语言中用到回调函数就想到指向函数的指针

//   void (*up)(char*)

void change(char *str, void (*up)(char*))

{

    (*up)(str);

//    1.把单词首字母转换为大写

    while (*str != '\0') {

//        如果是空格就取出下一个元素转换为大写

        if (*str == ' ') {

            str++;//移动到下一个元素

//            up(str);

            (*up)(str);

        }else{

            str++;

        }

    }

}

 

protocol(协议)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SEL数据类型

//  Person.h

#import

 

@interface Person : NSObject

 

- (void)eat;

- (void)call:(NSString *)number;

- (NSString *)findName;

 

@end

//  Person.m

#import "Person.h"

 

@implementation Person

 

- (void)eat

{

    NSLog(@"吃东西");

}

- (void)call:(NSString *)number

{

    NSLog(@"打电话给 %@",number);

}

- (NSString *)findName

{

    return @"找到了腾格尔";

}

 

@end

//  main.m

#import

 

#import "Person.h"

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

       

        Person * p = [[Person alloc] init];

       

        //消息机制

//        [p eat];

 

       

       

       

//        [p performSelector:@selector(eat)];// SEL使用的常见形式

       

        [p performSelector:@selector(abc)];

       

        [p eat];

       

       

    }

    return 0;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值