Objective-C 多重继承 [英] Objective-C multiple inheritance
本文介绍了Objective-C 多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个类,一个包括methodA,另一个包括methodB.所以在一个新类中,我需要覆盖方法 methodA 和 methodB.那么如何在目标 C 中实现多重继承呢?我对语法有点困惑.
I have 2 classes one includes methodA and the other include methodB. So in a new class I need to override the methods methodA and methodB. So how do I achieve multiple inheritance in objective C? I am little bit confused with the syntax.
推荐答案
Objective-C 不支持多重继承,你不需要它.使用组合:
Objective-C doesn't support multiple inheritance, and you don't need it. Use composition:
@interface ClassA : NSObject {
}
-(void)methodA;
@end
@interface ClassB : NSObject {
}
-(void)methodB;
@end
@interface MyClass : NSObject {
ClassA *a;
ClassB *b;
}
-(id)initWithA:(ClassA *)anA b:(ClassB *)aB;
-(void)methodA;
-(void)methodB;
@end
现在您只需要调用相关 ivar 上的方法.这是更多的代码,但在 Objective-C 中没有多重继承作为语言特性.
Now you just need to invoke the method on the relevant ivar. It's more code, but there just isn't multiple inheritance as a language feature in objective-C.
这篇关于Objective-C 多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文