阅读背景:

OC基础——使用category(策略)实现分类,在不改变原类代码的前提下为原类增加方法_liu537192的专栏

来源:互联网 
//
//  main.m
//  category
//
//  Created by mj on 13-4-4.
//  Copyright (c) 2013ๅนด itcast. All rights reserved.
//

#import 
    
     

#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 test];
        
        [stu test2];
        
        [stu test3];
        
        NSLog(@"%@", [NSString json]);
    }
    return 0;
}

//
//  Student.h
//  category
//
//  Created by mj on 13-4-4.
//  Copyright (c) 2013ๅนด itcast. All rights reserved.
//

#import 
     
      

@interface Student : NSObject
- (void)test;
@end
// 在Student.h声明一个Student的分类,不一定要在别的文件中声明分类。
// 用括号"()"来表示分类,括号中的Addition表示分类名
@interface Student(Addition)
- (void)test3;
@end
//
//  Student.m
//  category
//
//  Created by mj on 13-4-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Student.h"

@implementation Student
- (void)test {
    NSLog(@"调用了test方法");
}
@end
// 在Student.m文件中实现Student的这个分类
@implementation Student(Addition)
- (void)test3 {
    NSLog(@"调用了test3方法");
}
@end
//
//  Student+Test.h
//  category
//
//  Created by mj on 13-4-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Student.h"

// ()代表着是一个分类
// ()中的Test代表着分类的名称
@interface Student (Test)
// 分类只能扩展方法,不能增加成员变量

- (void)test2;
@end
//
//  Student+Test.m
//  category
//
//  Created by mj on 13-4-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Student+Test.h"

@implementation Student (Test)

- (void)test2 {
    NSLog(@"调用了test2方法");
}

@end

     
    //
//  main.m
//  category
//
//  Created 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: