首页 > 其他 > 详细

Object-C之set方法,get方法

时间:2014-10-22 10:09:43      阅读:187      评论:0      收藏:0      [点我收藏+]

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


Object-C之set方法,get方法

原文:http://blog.csdn.net/pengyuan_d/article/details/40370669

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