@interface PersonModel : NSObject
{
@private int age ;
@public float heigh;
@public NSString *name;
}
@property (nonatomic ,assign )int age ;
-(id) init;
-(id) initWithAge : (int) newAge;
+(PersonModel * ) person;
@end
//
@implementation PersonModel
@synthesize age;
-(id)init{
if (self = [super init]) {
age = 30;
heigh = 179.0;
name = @"jiachong";
}
return self ;
}
-(id)initWithAge:(int)newAge{
if (self = [super init]) {
age = newAge;
heigh = 179.0;
name = @"jiachong";
}
return self ;
}
+(PersonModel * ) person{
return [[self alloc] init];
}
@synthesize age; //编译器自动解释成 age的get、set方法实现。 在xcode4.5之后可以不写这句话
@end
上面红色部分为定义
调用方法
PersonModel *p = [[PersonModel alloc] init];
p->name = @"jiachong";
p.age = 29;
p->heigh = 179.0;
NSLog(@"name is %@ , age is %d , height is %f .",p->name,p.age,p->heigh);