歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS中多重繼承實現的折中方法

iOS中多重繼承實現的折中方法

日期:2017/3/1 9:51:27   编辑:Linux編程

我們知道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

Copyright © Linux教程網 All Rights Reserved