码迷,mamicode.com
首页 > 其他好文 > 详细

OC2_点语法(属性关键字)

时间:2015-06-16 18:58:12      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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;
}

 

OC2_点语法(属性关键字)

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4581198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!