OC2_点语法(属性关键字)

//

//  Dog.h

//  OC2_点语法(属性关键字)

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface Dog : NSObject

//属性关键字

//assgin(缺省)直接赋值 //基本类型 代理对象指针



//retain 先release 再 retain //除了基本类型 字符串类型 代理对象, 剩下的通常用 retain



//copy   先release 再 copy  //字符串对象用copy





//atomic(缺省),nonatomic

//原子性的      非原子性的



//readwrite(缺省) 展开对应的setter getter方法

//readonly 只展开对应的getter方法



//setter getter  修改setter getter方法的方法名

//点语法访问不受影响

//经典访问要使用新的方法名



@property (assign,atomic)NSInteger age;



@property (assign, nonatomic, readwrite)float weight;



@property (copy,nonatomic)NSString *name;



@property (assign,setter=setNewNumber:,getter=getNumber) NSInteger number;





@end
//

//  Dog.m

//  OC2_点语法(属性关键字)

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "Dog.h"



@implementation Dog



@synthesize age = _age;



- (NSInteger)age

{

    NSLog(@"getter");

    return _age;

}



- (void)setAge:(NSInteger)age

{

    NSLog(@"setter");

    _age = age;

}



@end
//

//  main.m

//  OC2_点语法(属性关键字)

//

//  Created by zhangxueming on 15/6/16.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Dog.h"



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

    @autoreleasepool {

        Dog *dog = [[Dog alloc] init];

        dog.name= @"小白";

        dog.age = 12;

        dog.weight = 34.5;

        NSLog(@"name = %@ age = %li weight = %.2f", dog.name, dog.age, dog.weight);

        

        dog.number = 100;

        NSLog(@"number = %li", dog.number);

        

        [dog setNewNumber:200];

        NSLog(@"number = %li", [dog getNumber]);

        

        

        

        

    }

    return 0;

}

 

你可能感兴趣的:(关键字)