OC第五节 ——点语法和@property

一、setter和getter函数
    1.回忆:如何访问对象中的成员变量
    2.setter和getter函数的作用
            setter  方法:   修改对象的字段/实例变量
            getter 方法:   读取对象的字段/实例变量
            
            setter 方法: 可以带有多个参数,可以同时给多个变量赋值

            getter方法: 不带参数,只能返回一个变量的值。


        
    3.setter和getter如何命名
                
    setter 方法的命名:

            xxxx: 表示的是成员变量的名字

            //带有一个参数
            - (void)setXxxx:(参数类型)参数1;
            //带有多个参数
            - (void)setXxxx:(参数类型)xxxx andXxxx:(参数类型)xxxx......;

            
    getter 方法的命名:
            - (返回值类型)xxxx;


       
=======================================
二、点语法
    1.为什么要设计点语法
        为了方便别的程序员转到OC上面来,   C ++, jave ,c#  都是使用的点语法        为了让程序设计简单化
        隐藏了内存管理细节
        隐藏了多线程、同步、枷锁细节
    

    2.点语法的作用
        属性可以在不适用括号的情况下使用点语法
        无需调用[foo value] 可以调用foo.value 来访问. 注意跟结构体的区别
        虽然foo.value 看起来像是直接访问value变量,但是属性始终调用
        方法,而这些方法又可以访问对象的数据。
        

    3.点语法的调用
        xiaohong.name = @“xiaohong”;这里实际上调用的是setter方法。
        NSString *nameString = xiaohong.name; 这里实际上调用的是getter方法。


    4.点语法和setter/getter函数的关系
        @interface QFDog: NSObject
        {
            NSInteger _age;
        }
        - (void)setAge:(NSInteger)newAge;
        - (NSInteger)age;
        @end

        @implementation QFDog
        - (void)setAge:(NSInteger)newAge
        {
                _age = newAge;
        }
        - (NSInteger)age
        {
                return _age;
        }
        @end

        int main(void)
        {
                QFDog *dog = [[QFDog alloc] init];
                [dog setAge:10];
                NSInteger dogAge  = [dog age];

                dog.age = 20;
                NSInteger dogAge1 = dog.age;

                return 0;
        }


【注】
         【点语法和[]写法】
            本质一样,同样是发送set和get消息,只不过写法不同。
         
         点语法:
            对象指针.实例变量;
         
            如果是赋值
                    对象指针.实例变量 = xxxx;
            展开成: [对象指针 setXxxx: xxxx];
         
            
            如果是取值:
                    xxxx = 对象指针.实例变量;
            展开成: [对象指针 实例变量];
         


           dog.name = @“xiaobai”;
    展开成: [dog setName: @“xiaobai”];
        

               NSString *nameString = dog.name;
    展开成: NSString *nameString = [dog  name];
         
         能够使用点语法的条件:
            1、必须实现set和get方法
            2、只能用于实例化的变量的赋值和取值
            3、能使用点语法的赋值,对应的set方法只能够带有一个参数

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *_name;
    NSInteger _age;
    NSString *_identical;
}
- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

- (void)setIdentical:(NSString *)identical;
- (NSString *)identical;
@end


#import "Person.h"

@implementation Person
- (void)setName:(NSString *)name
{
    _name = name;
}
- (NSString *)name
{
    return _name;
}
- (void)setAge:(NSInteger)age
{
    _age = age;
}
- (NSInteger)age
{
    return _age;
}

- (void)setIdentical:(NSString *)identical
{
    _identical = identical;
}
- (NSString *)identical
{
    return _identical;
}
@end

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

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

    @autoreleasepool {
        
        Person *p = [[Person alloc] init];
        p.name = @"李平";
        //[p setName:(NSString *)]
        NSString *nameStirng = p.name;
        //NSString *nameStirng = [p name];
        NSLog(@"name: %@", nameStirng);
        
        
        
        p.age = 10;
        p.identical = @"12345678";
        
        NSLog(@"age: %ld", p.age);
        NSLog(@"identical: %@", p.identical);
        
    }
    return 0;
}

 property

#import <Foundation/Foundation.h>

@interface MyPoint : NSObject
{
    NSInteger _x;
    NSInteger _y;
    NSInteger _z;
    NSString *_name;
}
@property NSInteger x, y, z;
//    NSInteger _x;
//    NSInteger _y;
//    NSInteger _z;



//展开成为下面的两行
//- (void)setX:(NSInteger)x;
//- (NSInteger)x;

//@property NSInteger y;
//- (void)setY:(NSInteger)y;
//- (NSInteger)y;


//@property NSInteger z;
//- (void)setZ:(NSInteger)z;
//- (NSInteger)z;

@property NSString *name;
//- (void)setName:(NSString *)name;
//- (NSString *)name;




- (void)test;

@end


#import "MyPoint.h"

@implementation MyPoint
//@synthesize x;  //首先会去实例变量列表查找x, 如果没有x, 生成x
@synthesize x = _x; //和成员列表的_x 进行关联
@synthesize y = _y;


- (void)test
{
//    NSLog(@"x = %ld", x);
    NSLog(@"_x = %ld", _x);
    NSLog(@"_y = %ld", _y);
    
}

//- (void)setX:(NSInteger)x
//{
//    _x = x;
//}
//
//
//- (NSInteger)x
//{
//    return _x;
//}

//如果自己实现了set和get方法,那么@synthesize就不会展开
//- (void)setY:(NSInteger)y
//{
//    NSLog(@"------------");
//    _y = y;
//}
//- (NSInteger)y
//{
//    return _y;
//}
//
//
//
//- (void)setZ:(NSInteger)z
//{
//    _z = z;
//}
//- (NSInteger)z
//{
//    return _z;
//}
//
//- (void)setName:(NSString *)name
//{
//    _name = name;
//}
//- (NSString *)name
//{
//    return _name;
//}
@end

#import <Foundation/Foundation.h>
#import "MyPoint.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {

        MyPoint *point = [[MyPoint alloc] init];
        
        point.x = 10;
        point.y = 100;
        
        [point test];
        NSLog(@"x : %ld, y: %ld", point.x, point.y);
        
        
    }

    return 0;
}
原文地址:https://www.cnblogs.com/laolitou-ping/p/6181567.html