Objective-C 类别使用

来源:互联网 发布:mac 系统怎么翻墙 编辑:程序博客网 时间:2024/05/21 10:01

1、目录结构:




2、CategoryThing.h:

////  CategoryThing.h//  consoloTest////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#ifndef CategoryThing_h#define CategoryThing_h#import <Foundation/Foundation.h>@interface CategoryThing : NSObject {    NSInteger thing1;    NSInteger thing2;    NSInteger thing3;}@end@interface CategoryThing (Thing1)- (void) setThing1:(NSInteger)thing1;- (NSInteger) thing1;@end@interface CategoryThing (Thing2)- (void) setThing2:(NSInteger)thing2;- (NSInteger) thing2;@end@interface CategoryThing (Thing3)- (void) setThing3:(NSInteger)thing3;- (NSInteger) thing3;@end#endif /* CategoryThing_h */


3、CategoryThing.m:

////  CategoryThing.m//  consoloTest////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "CategoryThing.h"@implementation CategoryThing- (NSString *) description {    NSString *desc;    desc = [NSString stringWithFormat:@"%ld %ld %ld", (long)thing1, (long)thing2, (long)thing3];    return (desc);}@end


4、Thing1.m:

////  Thing1.m//  consoloTest////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "CategoryThing.h"@implementation CategoryThing (Thing1)- (void) setThing1:(NSInteger)t1 {    thing1 = t1;}- (NSInteger) thing1 {    return (thing1);}@end


5、Thing2.m:

////  Thing2.m//  consoloTest////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "CategoryThing.h"@implementation CategoryThing (Thing2)- (void) setThing2:(NSInteger)t2 {    thing2 = t2;}- (NSInteger) thing2 {    return (thing2);}@end



6、Thing3.m:

////  Thing3.m//  consoloTest////  Created by 侯家奇 on 16/8/17.//  Copyright © 2016年 侯家奇. All rights reserved.//#import "CategoryThing.h"@implementation CategoryThing (Thing3)- (void) setThing3:(NSInteger)t3 {    thing3 = t3;}- (NSInteger) thing3 {    return (thing3);}@end



7、main.m:

#import "CategoryThing.h"int main (int argc, const char *argv[]) {    @autoreleasepool {        CategoryThing *thing = [[CategoryThing alloc] init];        [thing setThing1:5];        [thing setThing2:23];        [thing setThing3:42];        NSLog(@"Things are %@", [thing description]);    }    return (0);}


8、结果:


0 0
原创粉丝点击