黑马程序员技术交流社区
标题:
@property方法的介绍与使用
[打印本页]
作者:
mazhichao
时间:
2015-7-4 17:32
标题:
@property方法的介绍与使用
@property
@property是便一起的指令
@property告诉编译器声明属性的访问器(getter/setter)方法
1、@property 使用格式
@property 数据类型 方法名(set方法去掉set后的名字)
2、作用
1)在xcode4.4之前,用于帮助我们实现get/set方法的声明
#import <Foundation/Foundation.h>
@interface person : NSObject
{
NSString *_name;
int _age;
}
@property NSString *_name;
/*@property NSString *_name的作用就是代替下面语句
-(void)setName:(NSString*)name;
-(NSString*)name;
*/
@property int _age;
/*@property int _age的作用就是代替下面语句
-(void)setAge:(int)age;
-(int)age;
*/
@end
复制代码
#import "person.h"
@implementation person
//set方法
-(void)setName:(NSString*)name
{
_name = name;
}
-(void)setAge:(int)age
{
_age = age;
}
//get方法
-(NSString*)name
{
return _name;
}
-(int)age
{
return _age;
}
@end
复制代码
main函数的实现与正常实现一样,此处不做多余代码的粘贴
作者:
yangzhen5352
时间:
2015-7-4 17:36
赞(≧▽≦)一下……
作者:
mazhichao
时间:
2015-7-4 18:08
刚刚又学了@synthesize实现.m文件
#import "person.h"
@implementation person
@synthesize name; //帮助生成变量name
/*
-(void)setName:(NSString*)name
{
_name = name;
}
-(NSString*)name
{
return _name;
}*/
@synthesize age; //帮助生成变量age
/*
-(void)setAge:(int)age
{
_age = age;
}
-(int)age
{
return _age;
}*/
@end
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2