首页 > 其他 > 详细

@property_@synthesize 配套使用

时间:2016-02-05 01:46:17      阅读:208      评论:0      收藏:0      [点我收藏+]

@property

   类默认实现变量的get set方法

@synthesize 是指定那个变量的 get和set方法

eg:

.h文件中定义

类Student中含有两个 int age,和int _age;

#import <Foundation/Foundation.h>

@interface  Student:NSObject
{
    @public
    int _age;
    int age;
}
@property int age;

-(void)test;

@end

 

@property int age;

.m文件中则制定哪个变量的get set 方法

@synthesize age=_age;

#import "Student.h"

@implementation Student

@synthesize age=_age;  //指定变量_age

-(void)test
{
    NSLog(@"age=%d PK _age=%d",age,_age);
}

@end

 

//文件调用

Student *student=[Student new];

//获取_age的值

int _age=[student age];

int main(int argc, const char * argv[]) {
    @autoreleasepool {
    
        #pragma mark 实例化
        Student *stu=[Student new];
        #pragma mark- 调用get和set
        stu->age=10;
        [stu setAge:18];
        
        [stu test];
    }
    return 0;
}

 //运行结果

2016-02-04 22:31:16.144 test[1302:340773] age=10 PK _age=18

 

@property_@synthesize 配套使用

原文:http://www.cnblogs.com/nidongde/p/5182432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!