OC默认不支持多继承,可以利用以下几种方式间接的实现多继承;
- 类的组合
@interface ClassA : NSObject - (void)aMethod; //公开方法 @end @implementation ClassA - (void)aMethod { //ClassA实现 NSLog(@"ClassA - aMethod"); } @end @interface ClassB : NSObject - (void)bMethod; //公开方法 @end @implementation ClassB - (void)bMethod { //ClassB实现 NSLog(@"ClassB - bMethod"); } @end @interface ClassGroup() @property(strong, nonatomic) ClassA *clsA; @property(strong, nonatomic) ClassB *clsB; @end @implementation ClassGroup - (instancetype)init { if (self = [super init]) { self.clsA = [ClassA new]; self.clsB = [ClassB new]; } return self; } - (void)groupMethod { //ClassGroup实现 NSLog(@"ClassGroup - groupMethod"); [self.clsA aMethod]; [self.clsB bMethod]; }
调用:
ClassGroup *group = [ClassGroup new]; [group groupMethod];
运行结果:
ClassGroup - groupMethod ClassA - aMethod ClassB - bMethod
2. 通过协议实现
ProtocolGroup.h
@protocol Runner <NSObject> @property(strong, nonatomic) NSString *name; - (void)run; @end @protocol Flyer <NSObject> @property(strong, nonatomic) NSString *name; - (void)fly; @end //同时遵守Runner和Flyer协议 @interface ProtocolGroup : NSObject<Runner, Flyer> @end
ProtocolGroup.m
@interface ProtocolGroup() @end @implementation ProtocolGroup @synthesize name = _name; - (void)run { NSLog(@"run"); } - (void)fly { NSLog(@"fly"); } //也可直接使用 @synthesize name(重写getter和setter是非必要的) - (void)setName:(NSString *)name { _name = name; } - (NSString *)name { return _name; } @end
调用
ProtocolGroup *group = [ProtocolGroup new]; group.name = @"zhangsan"; [group fly]; [group run]; NSLog(@"%@", group.name);
运行结果:
fly run zhangsan
3. 通过分类实现
@interface CateGroup (Inherit) @property(strong, nonatomic) NSString *name; - (void)run; - (void)fly; @end #import <objc/runtime.h> @implementation CateGroup (Inherit) - (void)setName:(NSString *)name { objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)name { return objc_getAssociatedObject(self, _cmd); } - (void)run { NSLog(@"run"); } - (void)fly { NSLog(@"fly"); } - (void)custom { NSLog(@"自定义方法"); } @end
4. 消息转发
ClassGroup.h
@interface ClassA : NSObject - (void)aMethod; //公开方法 @end @interface ClassB : NSObject - (void)bMethod; //公开方法 @end @interface ClassGroup : NSObject @end
ClassGroup.m
@implementation ClassA - (void)aMethod { //ClassA实现 NSLog(@"ClassA - aMethod"); } @end @implementation ClassB - (void)bMethod { //ClassB实现 NSLog(@"ClassB - bMethod"); } @end @interface ClassGroup() @end @implementation ClassGroup - (id)forwardingTargetForSelector:(SEL)aSelector { ClassA *clsA = [ClassA new]; ClassB *clsB = [ClassB new]; if ([clsA respondsToSelector:aSelector]) { return clsA; }else if ([clsB respondsToSelector:aSelector]) { return clsB; } return nil; } @end
调用
ClassGroup *group = [ClassGroup new]; [group performSelector:NSSelectorFromString(@"aMethod")]; [(ClassB *)group bMethod];
此处也可利用以下两个方法处理,CateGroup可以继承自NSProxy更加轻量级:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { //doSomething } - (void)forwardInvocation:(NSInvocation *)anInvocation { //doSomething }