您的位置:首页 > 移动开发 > IOS开发

iOS开发之getter与setter方法

2015-12-07 21:38 591 查看
一.用property和synthesize分别进行成员变量的申明与实现

1.在xxx.h文件中用@property进行申明

//
//  Student.h
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject {
//默认是@protected 访问控制修饰符,此处用{}括起来,定义的是成员变量
int age;
}

//当编译器遇到@property时,会自动展开成getter和setter的声明
@property int age;
//相当于下面这两句
//- (void)setAge:(int)newAge;
//- (int) age;
@end


2.在xxx.m文件中用@synthesize进行实现

//
//  Student.m
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import "Student.h"

@implementation Student

//@synthesize写在@implementation与@end之间
@synthesize age;
//相当于下面的语句
//- (void)setAge:(int)newAge {
//    age = newAge;
//}
//
//- (int)age {
//    return age;
//}

@end


3.在main.m文件中进行调用

//
//  main.m
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [[Student alloc]init];

[stu setAge:10];

NSLog(@"age is %i",stu.age);
}
return 0;
}


二.如果成员变量定义为_age,则按如下步骤操作

1.进行成员变量的定义与申明

//
//  Student.h
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject {
//默认是@protected 访问控制修饰符
int _age;
}

//当编译器遇到@property时,会自动展开成getter和setter的声明
@property int age;
//相当于下面这两句
//- (void)setAge:(int)newAge;
//- (int) age;
@end


2.在.m文件中进行赋值

//
//  Student.m
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import "Student.h"

@implementation Student

//age=_age代表getter和setter回去访问_age这个成员变量
@synthesize age=_age;
//相当于下面这两句
//- (void)setAge:(int)newAge {
//    _age = newAge;
//}

//- (int)age {
//    return _age;
//}

@end


3.在main函数中进行调用

//
//  main.m
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [[Student alloc]init];

[stu setAge:10];

NSLog(@"age is %i",stu.age);
}
return 0;
}


补充:1.生成private成员变量

//
//  Student.h
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject

//下面定义的是私有成员变量 @private
//当编译器遇到@property时,会自动展开成getter和setter的声明
@property int age; //此句编译器会默认生成_age变量,所以在.m文件中需要这样赋值_age = xxx
//相当于下面这两句
//- (void)setAge:(int)newAge;
//- (int) age;
@end


2.生成protected成员变量

//
//  Student.h
//  property
//
//  Created by skythinking on 15/12/7.
//  Copyright © 2015年 skythinking. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject {
//默认是@protected 访问控制修饰符
int age;
}

//当编译器遇到@property时,会自动展开成getter和setter的声明
@property int age;
//相当于下面这两句
//- (void)setAge:(int)newAge;
//- (int) age;
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: