我們知道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;
}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()