Student.h:
#import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic) int no; -(void)TestStudent; @end
Student.m:
#import "Student.h" @implementation Student -(void)TestStudent{ NSLog(@"我是测试Student的方法"); } @end
Student+Test.h
#import "Student.h" //()分类文件方法 //Test是分类的方法名 @interface Student (Test) //注意:只能扩展方法,不能添加成员变量 -(void) Test2; @end @interface Student(Addtion) -(void) Test3; @end
Student+Test.m:
#import "Student+Test.h" @implementation Student (Test) -(void)Test2 { NSLog(@"我是分类方法Test2"); } @end @implementation Student(Addtion) -(void)Test3{ NSLog(@"我是分类方法test3"); } @end
NSString+JSON.h:
#import <Foundation/Foundation.h> @interface NSString (JSON) +(NSString *)json; @end
NSString+JSON.m:
#import "NSString+JSON.h" @implementation NSString (JSON) +(NSString *)json{ return @"{'name':'dxw','id':10}"; } @end
main:
#import <Foundation/Foundation.h> #import "Student.h" #import "Student+Test.h" #import "NSString+JSON.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[[Student alloc] init] autorelease]; [stu TestStudent]; [stu Test2]; [stu Test3]; NSLog(@"%@",[NSString json]); } return 0; }结果:
2013-08-02 15:47:57.364 Category[1322:303] 我是测试Student的方法
2013-08-02 15:47:57.366 Category[1322:303] 我是分类方法Test2
2013-08-02 15:47:57.367 Category[1322:303] 我是分类方法test3
2013-08-02 15:47:57.368 Category[1322:303] {'name':'dxw','id':10}