您的位置:首页 > 移动开发 > Objective-C

Object-C之set方法,get方法

2014-10-22 08:47 197 查看
Main.m

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

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

//创建person对象
Person *person = [[Person alloc] init];

//设置person对象的名字和年龄
[person setName:@"Jack"];
[person setAge:12];

//打印个人信息
[person showInfo];

//取得对象的信息
NSString *name = [person name];
NSInteger age = [person age];
NSLog(@"name:%@  age:%ld",name,age);

return 0;
}


Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject {

NSString *_name; //名字
NSInteger _age;  //年龄

}

//打印个人信息
- (void)showInfo;

/*________________对象的设置器方法(set方法)____________________*/

//设置名字的方法
- (void)setName:(NSString *)name;
//设置年龄
- (void)setAge:(NSInteger)age;

/*________________对象的访问器方法(get方法)____________________*/
//取出名字的方法get
- (NSString *)name;
//取出年龄的方法
- (NSInteger)age;

@end


Person.m

//打印个人信息
- (void)showInfo {

NSLog(@"这个人的个人信息:名字:%@  年龄:%ld",_name,_age);

}

//设置名字的方法
- (void)setName:(NSString *)name {
_name = name;

}

//设置年龄
- (void)setAge:(NSInteger)age {

_age = age;
}

//取出名字的方法get
- (NSString *)name {

return _name;
}

//取出年龄的方法
- (NSInteger)age {

return _age;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: