天天看点

【Objective-C】点语法

一、

1、 在java中,我们可以通过“对象名.成员变量名”来访问对象的公共成员变量,这个就是点语法。

public class Student {
         public int age;
}

public class Test {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.age = 10;

     }
}
           

学习java的时候知道比较正规的做法就是“让成员变量私有化,让外界使用公共的get方法和set方法访问成员变量”。

在oc中使用的点语法在语义中与java不太一样。

二、

1、传统的get方法与set方法 -- 在前面一篇博客中已经使用了这一方法(  )

#import <Foundation/Foundation.h>

@interface Student : NSObject {
    int age;
}

//下面是声明的是set与get
- (void)setAge:(int)newAge;
- (int)age;

@end
           
#import "Student.h"

@implementation Student

- (void)setAge:(int)newAge {  //实现的是set方法
    age = newAge;
}

- (int)age {       //实现的是get方法
    return age;
}

@end
           
#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *stu = [[Student alloc] init];
        
        // 设置age的值
        [stu setAge:10];
        
        // 取出age的值
        int age = [stu age];
        
        NSLog(@"age is %i", age);
        
        [stu release];
    }
    return 0;
}
           

上面的案例就是oc中比较原始的对set、get方法的使用,这个方法的调用都是使用中括号[]实现的,也是典型的oc方法。下面就是来看看OC引入的点语法。

2、 使用点语法代替传统的get、set方法

       下面的案例就是使用点语法来实现上面传统方法的案例:修改main函数里面

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        Student *stu = [[Student alloc] init];
        
        // 设置age的值
        stu.age = 10;           // 等价于[stu setAge:10];
        
        // 取出age的值
        int age = stu.age;      // 等价于int age = [stu age];
        
        NSLog(@"age is %i", age);
        
        [stu release];
    }
    
    return 0;
}
           

   1. stu.age = 10意思是,把原来的[stu setAge:10]替换成了stu.age = 10。听清楚了,这两种写法是完全等价的。即这里的stu.age并不是代表直接访问stu对象的成员变量age,而是编译器遇到stu.age = 10的时候会自动将代码展开成[stu setAge:10],这其实是编译器在后台做了相关的修改转换,并不是像我们加到的一样。可以这么说“点语法”访问的是一个方法而不是变量。

【Objective-C】点语法

如果是直接访问成员变量的话,OC中应该是这样的语法:stu->age,而不是stu.age。

   2. int age = stu.age;意思是,把原来的int age = [stu age]替换成了int age = stu.age。这两种写法又是完全等价的,stu.age并不是直接访问stu对象的成员变量age,而是编译器遇到int age = stu.age的时候会自动将代码展开成int age = [stu age]

【Objective-C】点语法

   3.因此,OC中点语法的含义跟Java是完全不一样的,OC点语法的本质是方法调用,不是直接访问成员变量。至于这个点语法代表的是get方法还是set方法,那就取决于你是取值还是设值,取值就是get方法(如int age = stu.age),设值就是set方法(如 stu.age = 10)。

3、 点语法与self的知识

    1、 在java中this关键字代表着方法的调用者(也就是我们说的当前的对象),就是说谁调用了这个方法,this就代表谁。

public void setAge(int newAge) {
         this.age = newAge;

}
           

    2、 OC中有个self关键字,作用跟this关键字类似。我们可能就会想这样写OC的set方法了:

- (void)setAge:(int)newAge {
    self.age = newAge;
    
}
           

这里的self代表着当前调用setAge:方法的对象。这样子写是会造成死循环。因为我在前面已经说过了,OC点语法的本质是方法调用,所以上面的代码相当于:

- (void)setAge:(int)newAge {
    [self setAge:newAge];
    
}
           

4、oc建议的变量命名

  如果是第一次接触OC的点语法,很多人可能会真的以为stu.age的意思是直接访问stu对象的成员变量age。其实,有一部分原因是因为我们在这里定义的Student类的成员变量名就叫做age。为了更好地区分点语法和成员变量访问,一般我们定义的成员变量会以下划线 _ 开头。比如叫做 _age 。

如:

#import <Foundation/Foundation.h>

@interface Student : NSObject {
    int _age;  <span style="font-family:courier new,courier;">//变量的名字</span>
}

- (void)setAge:(int)newAge;
- (int)age;

@end
           
#import "Student.h"

@implementation Student

- (void)setAge:(int)newAge {
    _age = newAge;
}

- (int)age {
    return _age;
}

@end
           

感谢(M个J博主)http://www.cnblogs.com/mjios/