iOS开发之getter与setter方法

一.用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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值