1、ASProperty.h
#import <Foundation/Foundation.h>
@interface ASProperty : NSObject
{
@protected
NSString *name;
@private
NSString *sid;
@public
unsigned int age;
unsigned int num;
}
//属性声明
//@property(attributes)数据类型 实例变量
//存取器方法实现
//@synthesize 实例变量1,实例变量2,...实例变量n;
@property(nonatomic,assign) NSString* name;
@property(nonatomic,assign) NSString* sid;
@property(nonatomic,assign) unsigned int age;
@property(nonatomic,getter=getNo,setter=setNo:) unsigned int num;
//线程相关
//atomic:原子性,默认,加锁,线程安全
//nonatomic:非原子性,不加锁,非线性安全
//只读属性
//assign
//readonly:只读,只有get方法
//readwrite:读写,get与set方法均产生
@end
2、ASProperty.m
#import "ASProperty.h"
@implementation ASProperty
@synthesize name=_name,age,sid,num;
-(NSString*)func
{
return _name = @"sdaf";
}
@end
3、mian.m
#import <Foundation/Foundation.h>
#include "ASProperty.h"
int main(int argc, const char * argv[]) {
//自动释放池
@autoreleasepool {
ASProperty* pro = [[ASProperty alloc] init];
pro.name = @"yxk";
pro.sid = @"sid";
pro.age = 24;
NSLog(@"%@, %@, %u",pro.name,pro.sid,pro.age);
[pro setName:@"wdg"];
[pro setSid:@"sid"];
[pro setAge:24];
[pro setNo:123];
NSLog(@"%@, %@, %u, %u",[pro name],[pro sid],[pro age],[pro getNo]);
NSLog(@"%@",[pro func]);
[pro release];
}
return 0;
}
打印结果
2014-12-19 15:02:53.437 test1[2244:85084] yxk, sid, 24
2014-12-19 15:02:53.438 test1[2244:85084] wdg, sid, 24, 123
2014-12-19 15:02:53.438 test1[2244:85084] sdaf