Object-C——核心语法(1)

 

一、点语法

   1、概念:  我们可以通过"对象名.成员变量名"来访问对象的公共成员变量,这个就称为"点语法"。

   2、本质: 点语法的本质还是方法调用

   3、基本用法:

 1 #import <Foundation/Foundation.h>
 2 #import "Person.h"
 3 
 4 int main(int argc, const char * argv[])
 5 {
 6     Person *p = [Person new];
 7     
 8  
 9     p.age = 10; // [p setAge:10];
10     
11     int a = p.age; // [p age];
12     
13     
14     p.name = @"Jack";
15     
16     NSString *s = p.name;
17     
18     NSLog(@"%@", s);
19     
20     return 0;
21 }

二、成员变量的作用域

     1、  @public : 在任何地方都能直接访问对象的成员变量
     2、  @private : 只能在当前类的对象方法中直接访问(@implementation中默认是@private)
     3、@protected : 可以在当前类及其子类的对象方法中直接访问  (@interface中默认就是@protected)
     4、@package : 只要处在同一个框架中,就能直接访问对象的成员变量
     5、 @interface和@implementation中不能声明同名的成员变量

     6、代码举例

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 {
 5     int _no;
 6     
 7     @public // 在任何地方都能直接访问对象的成员变量
 8     int _age;
 9     
10     
11     @private  // 只能在当前类的对象方法中直接访问
12     int _height;
13     
14     @protected // 能在当前类和子类的对象方法中直接访问
15     int _weight;
16     int _money;
17 }
18 
19 - (void)setHeight:(int)height;
20 - (int)height;
21 
22 - (void)test;
23 @end
 1 @implementation Person
 2 {
 3     int _aaa;// 默认就是私有
 4     
 5     @public
 6     int _bbb;
 7     // @implementation中不能定义和@interface中同名的成员变量
 8     // int _no;
 9 }
10 
11 - (void)test
12 {
13     _age = 19;
14     
15     _height = 20;
16     
17     _weight = 50;
18     
19     _aaa = 10;
20 }
21 
22 - (void)setHeight:(int)height
23 {
24     _height = height;
25 }
26 
27 - (int)height
28 {
29     return _height;
30 }
31 
32 @end

三、 @property和@synthesize
     1、@property:可以自动生成某个成员变量的setter和getter声明

1 @property int age;
2 @property int height;

这两句和下面的两段代码是等价的

1 - (void)setAge:(int)age;
2 - (int)age;
3 
4 
5 
6 - (void)setHeight:(int)height;
7 - (int)height;

     2、 @synthesize自动生成age的setter和getter实现,并且会访问_age这个成员变量

       基础用法:

 1 #import "Person.h"
 2 
 3 @implementation Person
 4 
 5 
 6 @synthesize age = _age;
 7 
 8 @synthesize height = _height;
 9 
10 @synthesize weight = _weight, name = _name;
11 
12 @end


四、id的用法

      id是万能指针,能指向或者操作任何OC对象

      用法举例

 1 #import <Foundation/Foundation.h>
 2 #import "Person.h"
 3 
 4 
 5 void test(id d)
 6 {
 7     
 8 }
 9 
10 int main(int argc, const char * argv[])
11 {
12 
13     @autoreleasepool {
14         Person *p = [Person new];
15   
16         
17         NSObject *o = [Person new];
18         
19         
20       
21    
22         id d = [Person new];
23         
24         [d setAge:10];
25         
26         [d setObj:@"321423432"];
27         
28         NSLog(@"%d", [d age]);
29     }
30     return 0;
31 }

Person的.h和.m文件

 1 //.h 文件
 2 #import <Foundation/Foundation.h>
 3 
 4 @interface Person : NSObject
 5 @property int age;
 6 @property id obj;
 7 @end
 8 //.m
 9 #import "Person.h"
10 
11 @implementation Person
12 
13 @end

五、构造方法
    1、构造方法:用来初始化对象的方法,是个对象方法,-开头

          构造过程:
            ①   分配存储空间  +alloc
            ②   初始化 -init

1  // 1.调用+alloc分配存储空间
2     // Person *p1 = [Person alloc];
3     // 2.调用-init进行初始化
4     // Person *p2 = [p1 init];
5     
6 //    // 调用-init进行初始化
7 //    Person *p3 = [Person new];

    2、重写构造方法的目的:为了让对象创建出来,成员变量就会有一些固定的值

    3、 重写构造方法的注意点
       ① 先调用父类的构造方法([super init])
       ② 再进行子类内部成员变量的初始化

 1 #import "Person.h"
 2 
 3 @implementation Person
 4 
 5 
 6 // 重写-init方法
 7 //- (id)init
 8 //{
 9 //    // 1.一定要调用回super的init方法:初始化父类中声明的一些成员变量和其他属性
10 //    self = [super init]; // 当前对象 self
11 //    
12 //    
13 //    // 2.如果对象初始化成功,才有必要进行接下来的初始化
14 //    if (self != nil)
15 //    { // 初始化成功
16 //        _age = 10;
17 //    }
18 //    
19 //    // 3.返回一个已经初始化完毕的对象
20 //    return self;
21 //}
22 
23 - (id)init
24 {
25     if ( self = [super init] )
26     { // 初始化成功
27         _age = 10;
28     }
29     
30     // 3.返回一个已经初始化完毕的对象
31     return self;
32 }
33 
34 @end

六、 自定义构造方法
       自定义构造方法的规范
        1、一定是对象方法,一定以 - 开头
        2、返回值一般是id类型
        3、方法名一般以initWith开

       使用方法:

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 @property NSString *name;
 5 @property int age;
 6 - (id)initWithName:(NSString *)name;
 7 
 8 - (id)initWithAge:(int)age;
 9 
10 - (id)initWithName:(NSString *)name andAge:(int)age;
11 
12 @end

 

 


 

     

posted @ 2015-03-22 19:54  某个人  阅读(233)  评论(0编辑  收藏  举报