Can the Object-c class inherit multiple? Can I implement multiple interfaces?
The class of object-c cannot inherit multiple, and multiple interfaces can be implemented to accomplish multiple inheritance of C + +, although OC is syntactically forbidden to use multiple inheritance for classes, but it allows multiple inheritance to be used in protocol compliance. So you can use protocols to achieve multiple inheritance. But the protocol can only provide the interface, and not provide the implementation way, if only want to inherit the base class interface, then abide by the multi-protocol is undoubtedly the best method, and not only need to inherit the interface, but also inherit its implementation, then the protocol is powerless. Multi-Protocol compliance is relatively simple, concrete implementation of the way here is not talking!
Now CLASSC need to inherit ClassA in MethodA, ClassB MethodB, the specific code implementation is:
Define ClassA and its MethodA
@interface Classa:nsobject {
}
-(void) MethodA;
@end
Define CLASSB and its METHODB
@interface Classb:nsobject {
}
-(void) MethodB;
@end
Define the CLASSC and the methoda,methodb it needs
@interface Classc:nsobject {
ClassA *a;
ClassB *b;
}
-(ID) Initwitha: (ClassA *) A B: (ClassB *) b;
-(void) MethodA;
-(void) MethodB;
@end
Note the implementation of the CLASSC
@implementation ClassC
-(ID) Initwitha: (ClassA *) A B: (ClassB *) b{
A=[[classa alloc] initwithclassa:a];//[a copy];
B=[[CLASSB alloc] initwithclassb:b];//[b copy];
}
-(void) methoda{
[A MethodA];
}
-(void) methodb{
[B MethodB];
}
Can the Object-c class inherit multiple? Can I implement multiple interfaces? How is it implemented?