//// main.m// 类别//// Created by on 14-10-6.// Copyright (c) 2014年 apple. All rights reserved.//#import#import "Person.h"int main(int argc, const char * argv[]) { @autoreleasepool { Person *person = [Person personWithName:@"jack"]; [person eat]; [person sleep]; [person play]; NSLog(@"Person's name is: %@", [person name]); } return 0;}
//// Person.h// 类别//// Created by on 14-10-6.// Copyright (c) 2014年 apple. All rights reserved.//#import@interface Person : NSObject{ NSString *name; int age;}- (void) test;- (NSString *) name;- (int) age;@end@interface Person (Creation)+ (id) personWithName:(NSString *)_name;+ (id) personWithName:(NSString *)_name withAge:(int)_age;- (id) initWithName:(NSString *)_name;- (id) initWithName:(NSString *)_name withAge:(int)_age;@end@interface Person (Life)- (void) eat;- (void) sleep;- (void) play;@end
//// Person.m// 类别//// Created by on 14-10-6.// Copyright (c) 2014年 apple. All rights reserved.//#import "Person.h"@implementation Person- (void) test { NSLog(@"Person本类的方法");} // test- (NSString *) name { return name;} // getName- (int) age { return age;} // getAge@end@implementation Person (Creation)+ (id) personWithName:(NSString *)_name { Person *person = [[Person alloc] init]; person->name = _name; return person;} // personWithName+ (id) personWithName:(NSString *)_name withAge:(int)_age { Person *person = [[Person alloc] init]; person -> name = _name; person -> age = _age; return person;} // personWithName:withAge:- (id) initWithName:(NSString *)_name { if (self = [super init]) { name = _name; } return self;} // initWithName- (id) initWithName:(NSString *)_name withAge:(int)_age { if (self = [super init]) { name = _name; age = _age; } return self;} // initWithName:withAge:@end@implementation Person (Life)- (void) eat { NSLog(@"正在吃饭");} // eat- (void) sleep { NSLog(@"正在睡觉");} // sleep- (void) play { NSLog(@"正在玩耍");} // play@end