关键词:
多继承
,Protocol
,Category
,Runtime
面试题:
1)OC中可以多继承吗?
2)多继承的实现方案有哪些?
一、概述
面向对象编程之所以成为主流的编程思想和他的继承和多态是分不开的,只要是面向对象语言都支持继承和多态,当然不同的OOP语言之间都有其特点。OC中和Java类似,不支持多重继承,但OOP语言C++就支持多继承。
为什么Objective-C不支持多继承?
Objective-C不支持多继承,由于消息机制名字查找发生在运行时而非编译时,很难解决多个基类可能导致的二义性问题。
二、OC的多继承实现方案
虽然OC不支持多继承,但是可以通过其他方式间接地达到多继承的效果。
- 通过组合实现多继承;
- 通过协议实现多继承;
- 通过类别实现多继承;
- 通过Runtime消息转发实现多继承;
- 通过NSProxy实现多继承;
代码演示如下:
1、通过组合实现多继承
ClassA:
// .h
@interface ClassA : NSObject
- (void)funcA;
@end
// .m
#import "ClassA.h"
@implementation ClassA
- (void)funcA{
NSLog(@"%s",__func__);
}
@end
ClassB:
// .h
@interface ClassB : NSObject
- (void)funcB;
@end
// .m
#import "ClassB.h"
@implementation ClassB
- (void)funcB{
NSLog(@"%s",__func__);
}
@end
ClassC:(通过组合多个类来间接实现多继承)
// .h
@interface ClassC : NSObject
- (void)funcA;
- (void)funcB;
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
@interface ClassC ()
@property (nonatomic, strong) ClassA *clsA;
@property (nonatomic, strong) ClassB *clsB;
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
- (void)funcA {
[self.clsA funcA];
}
- (void)funcB{
[self.clsB funcB];
}
@end
2、通过协议实现多继承
ClassA:
// .h
@protocol ClassAProtocol <NSObject>
- (void)funcA;
@end
@interface ClassA : NSObject<ClassAProtocol>
@end
// .m
#import "ClassA.h"
@implementation ClassA
- (void)funcA{
NSLog(@"%s",__func__);
}
@end
ClassB:
// .h
@protocol ClassBProtocol <NSObject>
- (void)funcB;
@end
@interface ClassB : NSObject<ClassBProtocol>
@end
// .m
#import "ClassB.h"
@implementation ClassB
- (void)funcB{
NSLog(@"%s",__func__);
}
@end
ClassC:(通过遵循多个协议来间接实现多继承)
// .h
@interface ClassC : NSObject
- (void)funcA;
- (void)funcB;
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
@interface ClassC ()<ClassAProtocol, ClassBProtocol>
@property (nonatomic, strong) ClassA *clsA;
@property (nonatomic, strong) ClassB *clsB;
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
- (void)funcA {
[self.clsA funcA];
}
- (void)funcB{
[self.clsB funcB];
}
@end
3、通过类别实现多继承
ClassA:
// .h
@interface ClassA : NSObject
- (void)funcA;
@end
// .m
#import "ClassA.h"
@implementation ClassA
- (void)funcA{
NSLog(@"%s",__func__);
}
@end
ClassB:
// .h
@interface ClassB : NSObject
- (void)funcB;
@end
// .m
#import "ClassB.h"
@implementation ClassB
- (void)funcB{
NSLog(@"%s",__func__);
}
@end
ClassC:(通过添加多个类别来间接实现多继承)
// .h
@interface ClassC : NSObject
- (void)funcA;
- (void)funcB;
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
#import "ClassC+A.h"
#import "ClassC+B.h"
@interface ClassC ()
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
- (void)funcA {
[self.clsA funcA];
}
- (void)funcB{
[self.clsB funcB];
}
@end
ClassC+A:
// .h
#import "ClassC.h"
@class ClassA;
@interface ClassC (A)
@property (nonatomic, strong) ClassA *clsA;
- (void)funcA;
@end
// .m
#import "ClassC+A.h"
#import "ClassA.h"
#import <objc/runtime.h>
@implementation ClassC (A)
- (void)setClsA:(ClassA *)clsA{
objc_setAssociatedObject(self, @selector(clsA), clsA, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (ClassA *)clsA{
return objc_getAssociatedObject(self, @selector(clsA));
}
- (void)funcA{
[self.clsA funcA];
}
@end
ClassC+B:
// .h
#import "ClassC.h"
@class ClassB;
@interface ClassC (B)
@property (nonatomic, strong) ClassB *clsB;
- (void)funcB;
@end
// .m
#import "ClassC+B.h"
#import "ClassB.h"
#import <objc/runtime.h>
@implementation ClassC (B)
- (void)setClsB:(ClassB *)clsB{
objc_setAssociatedObject(self, @selector(clsB), clsB, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (ClassB *)clsB{
return objc_getAssociatedObject(self, @selector(clsB));
}
- (void)funcB{
[self.clsB funcB];
}
@end
4、通过Runtime消息转发实现多继承
Runtime在经历「消息传递」阶段仍未找到方法的实现时,会进入「消息转发」阶段。大致的流程如下:
想了解更多Runtime相关知识,可以看看这篇:「重拾iOS-Runtime」
「消息转发」
消息转发会经历三个过程:
1)首先是「动态方法解析」,Objective-C运行时会调用 resolveInstanceMethod:
,让你有机会提供一个函数实现。如果你添加了函数并返回YES, 那运行时系统就会重新启动一次消息发送的过程。
2)然后是「备用接收者」,如果目标对象实现了forwardingTargetForSelector:
,RunTime 这时就会调用这个方法,给你把这个消息转发给其他对象的机会。
3)最后是「完整的消息转发」,首先它会发送methodSignatureForSelector:
消息获得函数的参数和返回值类型。如果methodSignatureForSelector:
返回nil ,Runtime则会发出 doesNotRecognizeSelector:
消息,程序这时也就挂掉了。如果返回了一个函数签名,Runtime就会创建一个NSInvocation 对象并发送 forwardInvocation:
消息给目标对象。
所以说我们有三次机会,去间接的实现多继承。但是可想而知,越到后面的阶段,代价也就越大。
ClassA和ClassB不变
ClassA:
// .h
@interface ClassA : NSObject
- (void)funcA;
@end
// .m
#import "ClassA.h"
@implementation ClassA
- (void)funcA{
NSLog(@"%s",__func__);
}
@end
ClassB:
// .h
@interface ClassB : NSObject
- (void)funcB;
@end
// .m
#import "ClassB.h"
@implementation ClassB
- (void)funcB{
NSLog(@"%s",__func__);
}
@end
1)通过「动态方法解析」来实现多继承
ClassC:
// .h
@interface ClassC : NSObject
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
#import "objc/runtime.h"
@interface ClassC ()
@property (nonatomic, strong) ClassA *clsA;
@property (nonatomic, strong) ClassB *clsB;
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
// MARK: 动态方法解析
// 对象方法
+ (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(funcA)) {
Method method = class_getInstanceMethod(self, @selector(resolvedFuncA));
class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
return YES;
}
if (sel == @selector(funcB)) {
Method method = class_getInstanceMethod(self, @selector(resolvedFuncB));
class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method));
return YES;
}
return [super resolveInstanceMethod:sel];
}
// 类方法
+ (BOOL)resolveClassMethod:(SEL)sel {
return [super resolveClassMethod:sel];
}
- (void)resolvedFuncA {
[self.clsA funcA];
}
- (void)resolvedFuncB {
[self.clsB funcB];
}
@end
调用方法
// 使用performSelector调用方法
ClassC *clsC = [[ClassC alloc]init];
[clsC performSelector:NSSelectorFromString(@"funcA") withObject:nil];
[clsC performSelector:NSSelectorFromString(@"funcB") withObject:nil];
2)通过「备用接收者」来实现多继承
ClassC:
// .h
@interface ClassC : NSObject
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
#import "objc/runtime.h"
@interface ClassC ()
@property (nonatomic, strong) ClassA *clsA;
@property (nonatomic, strong) ClassB *clsB;
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
// MARK: 备用接受者
- (id)forwardingTargetForSelector:(SEL)aSelector {
if (aSelector == @selector(funcA) && [self.clsA respondsToSelector:aSelector]) {
return self.clsA;
}
if (aSelector == @selector(funcB) && [self.clsB respondsToSelector:aSelector]) {
return self.clsB;
}
return [super forwardingTargetForSelector:aSelector];
}
@end
3)通过「完整的消息转发」来实现多继承
ClassC:
// .h
@interface ClassC : NSObject
@end
// .m
#import "ClassC.h"
#import "ClassA.h"
#import "ClassB.h"
#import "objc/runtime.h"
@interface ClassC ()
@property (nonatomic, strong) ClassA *clsA;
@property (nonatomic, strong) ClassB *clsB;
@end
@implementation ClassC
- (instancetype)init{
if (self = [super init]) {
self.clsA = [[ClassA alloc]init];
self.clsB = [[ClassB alloc]init];
}
return self;
}
// MARK: 完整的消息转发
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
if (aSelector == @selector(funcA) && [self.clsA respondsToSelector:aSelector]) {
return [self.clsA methodSignatureForSelector:aSelector];
}
if (aSelector == @selector(funcB) && [self.clsB respondsToSelector:aSelector]) {
return [self.clsB methodSignatureForSelector:aSelector];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
SEL aSelector = [anInvocation selector];
if (aSelector == @selector(funcA) && [self.clsA respondsToSelector:aSelector]) {
[anInvocation invokeWithTarget:self.clsA];
}
else if (aSelector == @selector(funcB) && [self.clsB respondsToSelector:aSelector]) {
[anInvocation invokeWithTarget:self.clsB];
}
else {
[super forwardInvocation:anInvocation];
}
}
@end
5、通过NSProxy实现多继承
SFProxy:
// .h
@interface SFProxy : NSProxy
-(void)transformToObject:(NSObject *)obj;
@end
// .m
#import "SFProxy.h"
@interface SFProxy ()
@property (nonatomic, strong) NSObject *obj;
@end
@implementation SFProxy
-(void)transformToObject:(NSObject *)obj {
self.obj = obj;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
if (self.obj && [self.obj respondsToSelector:aSelector]) {
return [self.obj methodSignatureForSelector:aSelector];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
SEL aSelector = [anInvocation selector];
if (self.obj && [self.obj respondsToSelector:aSelector]) {
[anInvocation invokeWithTarget:self.obj];
}
else {
[super forwardInvocation:anInvocation];
}
}
@end
方法调用:
ClassA *clsA = [[ClassA alloc]init];
ClassB *clsB = [[ClassB alloc]init];
SFProxy *proxy = [SFProxy alloc];
// 变身为clsA的代理
[proxy transformToObject:clsA];
[proxy performSelector:@selector(funcA) withObject:nil];
// 变身为clsB的代理
[proxy transformToObject:clsB];
[proxy performSelector:@selector(funcB) withObject:nil];
打印:
2020-07-04 12:44:49.893660+0800 OCTestDemo[71379:1458448] -[ClassA funcA]
2020-07-04 12:44:49.893836+0800 OCTestDemo[71379:1458448] -[ClassB funcB]
好了,以上就是间接实现多继承的方案。综合来讲,个人觉得还是「通过协议来实现多继承」较为直观。