iOS中多重继承实现的折中方法

我们知道OC是不支持多重继承的,那么我们希望ClassA的某些子类在某种情况下又能调用ClassB中的方法该这么办呢,有人说使用Protocal啊,那么你会发现,当你想增加一个方法的调用时,还是需要到ClassA和ClassB中去维护两份代码。而且你必须要修改ClassA和ClassB去conform 你的protocal。

我们希望在不污染ClassA 和 ClassB的基础上使得ClassA的子类能够同时拥有ClassA和ClassB的方法(MethodA和MethodB),我认为category更适合这样的情景。

首先ClassA

@interface GTClassA : NSObject
– (void)methodA;
@end

@implementation GTClassA
– (void)methodA
{
    NSLog(@”this is methodA”);
}

@end

然后是ClassB

@interface GTClassB : NSObject
– (void)methodB;
@end

@implementation GTClassB
– (void)methodB
{
    NSLog(@”this is methodB”);
}

– (void)dealloc
{
    NSLog(@”dealloc in %@”,[self class]);
    [super dealloc];
}
@end

我们将采用的方法是 构建一个ClassA的Category的方式,但这种方式有一个缺点,不能定义成员变量。如果MethodB中的方法需要回调,那么我们需要保持一个ClassB的Instance,但何时释放不造成内层泄露就成了问题,那先看完不需要回调的实现,我们再讨论吧:

//  Created by Guangtao Li on 13-11-10.
//  Copyright (c) 2013年 Guangtao Li. All rights reserved.
//

#import “GTClassA.h”
@interface GTClassA (ClassBInheritance)
@end

//  Created by Guangtao Li on 13-11-10.
//  Copyright (c) 2013年 Guangtao Li. All rights reserved.
//
#import “GTClassA+ClassBInheritance.h”
#import “GTClassB.h”

@implementation GTClassA (ClassBInheritance)
-(void)forwardInvocation:(NSInvocation *)anInvocation{
    if([self respondsToSelector:[anInvocation selector]]){
        [anInvocation invokeWithTarget:self];
    }else{
        GTClassB *classBInstance = [[GTClassB alloc] init];
        [anInvocation invokeWithTarget:classBInstance];
        [classBInstance release];//立刻释放了
    }
}

– (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
    NSMethodSignature* signature = [super methodSignatureForSelector:selector];
    if (!signature) {
        signature = [GTClassB instanceMethodSignatureForSelector:selector];
    }
    return signature;
}

-(BOOL)conformsToProtocol:(Protocol *)aProtocol{
    if([GTClassB conformsToProtocol:aProtocol] || [super conformsToProtocol:aProtocol]){
        return YES;
    }
    return NO;
}

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2013-11/92633p2.htm

推荐阅读:

iOS 7 Beta 2发布,支持iPad和iPad Mini http://www.linuxidc.com/Linux/2013-06/86483.htm

iOS7 Beta 1测试版下载地址 http://www.linuxidc.com/Linux/2013-06/85836.htm

iOS 7 beta 【下载 + 教程+体验】适用 iPhone 5、4S、4、iPod touch 5 代 http://www.linuxidc.com/Linux/2013-06/85847.htm

抢先体验 苹果iOS7 beta测试版激活教程 http://www.linuxidc.com/L