黑马程序员——OC中的核心语法

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、点语法

1、什么是点语法?

@点语法其实就是方法调用,对普通方法

@当使用点语法时,编译器会自动展开成相应的方法

示例

[objc]  view plain copy
  1.     Person *p = [Person new];  
  2.       
  3.     // 点语法的本质还是方法调用  
  4.     p.age = 10// [p setAge:10];  
  5.     int a = p.age// [p age];  
  6.      
  7.     p.name = @"Jack";  
  8.     NSString *s = p.name;  
  9.     NSLog(@"%@", s);  
2、点语法使用注意

一下方法会引发死循环,因为方法调用了自己

[objc]  view plain copy
  1. - (void)setAge:(int)age  
  2. {  
  3.     //_age = age;  
  4.     NSLog(@"setAge:");  
  5.     // 会引发死循环  
  6.     //self.age = age; // [self setAge:age];  
  7. }  
  8.   
  9. - (int)age  
  10. {  
  11.     NSLog(@"age");  
  12.     return _age;  
  13.     // 会引发死循环  
  14.     //return self.age;// [self age];  
  15. }  
二、成员变量的作用域

1、成员变量有四种作用域,分别为@protected、@public、@private、@package

2、解释

@protected:默认成员变量类型,只能在当前类的对象方法中直接访问

@protected:可以在当前类以及子类的对象方法中直接访问

@public:任何地方都可以直接访问,非常不安全,一般不使用

@package:同一个“体系内”(框架)可以访问,介于@private和@public之间,(暂时很少用到)

三、set方法和get方法的快速生成

set方法和get方法的代码重复性书写大大降低了程序员工作的效率

OC语法中使用@property和@synthetic方法大大简化了set方法和get方法的定义和声明

@property:

在.h文件中的@interface中使用,用来声明(和定义)set和get方法

示例:

[objc]  view plain copy
  1. @property int age;  
  2. //- (void)setAge:(int)age;  
  3. //- (int)age;  
  4.   
  5.   
  6. @property int height;  
  7. //- (void)setHeight:(int)height;  
  8. //- (int)height;  
  9.   
  10. @property double weight;  
  11.   
  12. @property NSString *name;  
@synthetic

在.m文件中的@implementation中使用,用来实现set和get方法

[objc]  view plain copy
  1. @synthesize age = _age;  
  2. //就可以代替  
  3. //- (int)age{  
  4. //    return _age;  
  5. //}  
@synthetic使用注意

如果已经使用了@property方法可以不使用synthetic方法

四、万能指针,可以指向任何OC对象类型的指针

[objc]  view plain copy
  1. //定义  
  2. id p = [Person new];  
五、构造方法

1、为什么要使用构造方法:为了让对象创建出来,成员变量就会有一些固定的值

Person *p = [Person new];显然不能满足这个需求

2、够着方法的初步认识:

@调用+alloc分配存储空间

@调用-init进行初始化

@合并起来可以完整的创建一个对象

[objc]  view plain copy
  1. Person *p1 = [Person alloc];  
  2. Person *p2 = [p1 init];  
  3.   
  4. Person *p4 = [[Person alloc] init];  
3、重写一个init方法初始化成员变量的值
@ 一定要调用回 super init 方法 : 初始化父类中声明的一些成员变量和其他属性

@如果对象初始化成功,才有必要进行接下来的初始化

@返回一个已经初始化完毕的对象

示例:

[objc]  view plain copy
  1. - (id)init  
  2. {  
  3.     if ( self = [super init] )  
  4.     { // 初始化成功  
  5.         _age = 10;  
  6.     }  
  7.     // 返回一个已经初始化完毕的对象  
  8.     return self;  
  9. }  
4、自定义构造方法

自定义构造方法的规范

@一定是对象方法,一定以 - 开头

@返回值一般是id类型

@方法名一般以initWith开头

实例演示:

@自定义构造方法的声明Person类

定义name和age成员变量并生成相应的构造方法和能够初始化两个成员变量的构造方法

[objc]  view plain copy
  1. - (id)initWithName:(NSString *)name;  
  2.   
  3. - (id)initWithAge:(int)age;  
  4.   
  5. - (id)initWithName:(NSString *)name andAge:(int)age;  
@自定义构造方法的实现Person类

[objc]  view plain copy
  1. - (id)initWithName:(NSString *)name  
  2. //能够初始化name的构造方法  
  3. {  
  4.     if ( self = [super init] )  
  5.     {  
  6.         _name = name;  
  7.     }  
  8.     return self;  
  9. }  
  10.   
  11. - (id)initWithAge:(int)age  
  12. //能够初始化age的构造方法  
  13. {  
  14.     if ( self = [super init] )  
  15.     {  
  16.         _age = age;  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (id)initWithName:(NSString *)name andAge:(int)age  
  22. //能够同时初始化name和age的构造方法  
  23. {  
  24.     if ( self = [super init] )  
  25.     {  
  26.         _name = name;  
  27.         _age = age;  
  28.     }  
  29.     return self;  
  30. }  

5子类的继承和实现

定义一个Student类继承Person对象并增加一个no成员变量和一个能同时初始化3个成员变量的构造方法

[objc]  view plain copy
  1. #import "Person.h"  
  2. @interface Student : Person  
  3. //继承Person类  
  4. @property int no;  
  5. - (id)initWithNo:(int)no;  
  6. - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no;  
  7. //新增变量和构造方法  
  8. @end  
子类方法的实现过程
[objc]  view plain copy
  1. #import "Student.h"  
  2.   
  3. @implementation Student  
  4. - (id)initWithNo:(int)no  
  5. {  
  6.     if ( self = [super init] )  
  7.     {  
  8.         _no = no;  
  9.     }  
  10.     return self;  
  11. }  
  12.   
  13. // 父类的属性交给父类方法去处理,子类方法处理子类自己的属性  
  14. - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no  
  15. {  
  16.     // 将name、age传递到父类方法中进行初始化  
  17.     if ( self = [super initWithName:name andAge:age])  
  18.     {  
  19.         _no = no;  
  20.     }  
  21.         return self;  
  22. }  
  23. @end  
六、分类

1、用法:在不改变原来类的情况下增加新的补充有两种方法

1)分类:分类只能增加方法

2)继承:继承可以增加成员变量和方法

2、格式:@interface 类名 (分类)

//方法声明

@end
@implementation 类名 (分类)

//方法的实现

@end

3、用途在开发一个庞大的类的时候一个类有多个人开发,有利于团队合作

实例演示->分类的应用:

 NSString增加一个类方法:计算某个字符串中阿拉伯数字的个数

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

@分类的声明

[objc]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. //.h文件  
  3. @interface NSString (Number)  
  4. + (int)numberCountOfString:(NSString *)str;  
  5. //类方法  
  6. - (int)numberCount;  
  7. //对象方法  
  8. @end  
@分类的实现

[objc]  view plain copy
  1. #import "NSString+Number.h"  
  2. //.m文件  
  3. @implementation NSString (Number)  
  4. + (int)numberCountOfString:(NSString *)str  
  5. {  
  6.     return [str numberCount];  
  7. //对象方法被类方法调用  
  8. }  
  9. - (int)numberCount  
  10. {  
  11.     int count = 0;  
  12.     for (int i = 0; i<self.length; i++)  
  13.     {  
  14.         // 取出i这个位置对应的字符  
  15.         unichar c = [self characterAtIndex:i];  
  16.         // 如果这个字符是阿拉伯数字  
  17.         if ( c>='0' && c<='9' )  
  18.         {  
  19.             count++;  
  20.         }  
  21.     }  
  22.     return count;  
  23. }  
  24. @end  

七、类的本质

类的本质就是一个Class类型的对象“类对象”

typedef struct objc_class *Class

八、description方法

1、什么是description方法?当NSLog调用某个类对象或者对象时就会调用类的discretion方法

2、description方法的注意,不要在NSLog中使用self,会进入死循环。

3、实例演示:下面方法输出一个字符串Abc

[objc]  view plain copy
  1. //类.m中的实现  
  2. + (NSString *)description  
  3. {  
  4.     return @"Abc";  
  5. }  
  6.   
  7. //mian.m文件中的调用  
  8. Person *p = [[Person alloc] init];  
  9. NSLog(@"%@", p);//默认情况下是输出<类 地址>  
如何使这个输出改为age=?,name=?

修改上述代码中的类.m文件中的description方法即可

[objc]  view plain copy
  1. - (NSString *)description  
  2. {  
  3.     return [NSString stringWithFormat:@"age=%d,name=%@", _age, _name]  
  4. }  
九、sel方法

1、sel方法的用途

@每个类方法的列表都存储在类对象中

@每个方法都有与之相对应的sel类型对象

@根据sel对象就可以找到方法的地址,进而调用方法

2、sel方法的创建

SEL s1 = @selector(test);

[p performSelector:@selector(test)];


十、习题练习

练习题1

[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 题目:改错1 
  3.  ****************************************************************************/  
  4.   
  5. #import <Foundation/Foundation.h>  
  6.   
  7. @interface Person : NSObject  
  8. @property int age;  
  9. @end  
  10.   
  11. @implementation Person  
  12. //+ (NSString *)description  
  13. //{  
  14. //    return [NSString stringWithFormat:@"_age=%d", _age];  
  15. //}  
  16.   
  17. //1.类方法不可以调用成员变量  
  18. - (NSString *)description  
  19. {  
  20.     return [NSString stringWithFormat:@"_age=%d", _age];  
  21. }  
  22. @end  
练习题2
[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 题目:改错2 
  3.  ****************************************************************************/  
  4.   
  5. #import <Foundation/Foundation.h>  
  6.   
  7. @interface Person : NSObject  
  8. - (void)test;  
  9. @property int age;  
  10. @end  
  11.   
  12. @implementation Person  
  13. - (void)test  
  14. {  
  15. //1.死循环  
  16. //    [self performSelector:_cmd];  
  17. }  
  18. @end  
  19.   
  20. int main()  
  21. {  
  22. //  id *p = [[Person alloc] init];  
  23. //    2.id后面不能加*(自带*)  
  24.     id p = [[Person alloc] init];  
  25.     [p setAge:10];  
  26. //    p.age = 10;  
  27. //    3.id类型对象不能使用点语法(自带*)  
  28.       
  29.     Class c = [Person class];  
  30. //    [c test];  
  31. //    4.c类对象应该调用类方法而不是对象方法  
  32.       
  33.     return 0;  
  34. }  
[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 题目:分析题1 
  3.  ****************************************************************************/  
  4.   
  5. #import <Foundation/Foundation.h>  
  6. //Persong类的声明,定义了一个age成员变量  
  7. @interface Person : NSObject  
  8. @property int age;  
  9. @end  
  10.   
  11. @implementation Person  
  12. //实现了一个类description方法输出A  
  13. + (NSString *)description  
  14. {  
  15.     return @"A";  
  16. }  
  17. //实现了一个对象description方法输出B  
  18. - (NSString *)description  
  19. {  
  20.     return @"B";  
  21. }  
  22. @end  
  23.   
  24. int main()  
  25. {  
  26.     Person *p = [Person new];  
  27.     Person *p2 = [[Person class] new];  
  28.     NSLog(@"%@", p);  
  29. //    输出B  
  30.     NSLog(@"%@", p2);  
  31. //    输出B  
  32.     NSLog(@"%@", [Person class]);  
  33. //    输出A  
  34.     NSLog(@"%@", [p class]);  
  35. //    输出A  
  36.     return 0;  
  37. }  
分析题2:
[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 题目:分析题2 
  3.  ****************************************************************************/  
  4. //2.类的加载过程  
  5.   
  6. #import <Foundation/Foundation.h>  
  7. @interface Person : NSObject  
  8. @end  
  9. //声明Person类  
  10. @interface Student : Person  
  11. @end  
  12. //声明Student类  
  13. @interface GoodStudent : Student  
  14. @end  
  15. //声明Student类的子类GoodStudent;  
  16.   
  17. //主函数  
  18. int main()  
  19. {  
  20.     Student *s = [[Student alloc] init];  
  21.     NSLog(@"%@", s);  
  22. //    1、先调用父类的description方法,再调用类本身的description方法,再调用分类的description方法  
  23. //    2、先调用description方法再调用initialize方法,调用initialize方法次序如上  
  24. //    3、最后输出系统默认的<类: 类的地址>  
  25.     return 0;  
  26. }  
  27.   
  28. //1、Student类的实现  
  29. @implementation Student  
  30. + (void)load  
  31. {  
  32.     NSLog(@"Student+load");  
  33. }  
  34. + (void)initialize  
  35. {  
  36.     NSLog(@"Student+initialize");  
  37. }  
  38. @end  
  39.   
  40. //2、GoodStudent (MJ)分类的实现  
  41.   
  42. @implementation GoodStudent(MJ)  
  43. + (void)load  
  44. {  
  45.     NSLog(@"GoodStudent-MJ+load");  
  46. }  
  47. + (void)initialize  
  48. {  
  49.     NSLog(@"GoodStudent-MJ+initialize");  
  50. }  
  51. @end  
  52.   
  53. //3、GoodStudent类的实现  
  54. @implementation GoodStudent  
  55. + (void)load  
  56. {  
  57.     NSLog(@"GoodStudent+load");  
  58. }  
  59. + (void)initialize  
  60. {  
  61.     NSLog(@"GoodStudent+initialize");  
  62. }  
  63. @end  
  64.   
  65. //4、Person类的实现  
  66. @implementation Person  
  67. + (void)load  
  68. {  
  69.     NSLog(@"Person+load");  
  70. }  
  71. + (void)initialize  
  72. {  
  73.     NSLog(@"Person+initialize");  
  74. }  
  75. @end  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值