**1. The use of combination, the need to inherit several objects declared as their own several global variables. However, the dog can be directly called in the corresponding method. **

2. Tundo complies with multiple protocols, but still has to provide its own implementation. It’s not multiple inheritance in principle.

3. Classification can be considered, because it will not destroy the sorting of metaclasses. It has a higher priority and is also recommended by Apple.

4. Message forwarding mechanism

Specific examples:

MethodA, methodB: methodA, methodB: methodA, methodB: methodA, methodB: methodA, methodB: methodA, methodB: methodA, methodB: methodA, methodB

// define ClassA and its methodA@interface ClassA: NSObject {}-(void)methodA; @end// define ClassB and its methodB@interface ClassB: NSObject {}-(void)methodB; @end// define ClassC and its required methodA, methodB@interface ClassC: NSObject {ClassA *a; ClassB *b; }-(id)initWithA:(ClassA *)A b:(ClassB *)B; -(void)methodA; -(void)methodB; @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]; }Copy the code

Method 2. Protocol Sets the ClassA delegate and the ClasssB delegate, and implements methodA and methodB in ClassA. ClassC complies with both protocols. [Protocol supports multiple inheritance]

Method 3. Category (Classification)

The ClassC class can implement methodA of ClassA and methodB of ClassB, so that ClassC can call methodA and methodB

Method 4. Message forwarding mechanism

We know that the way to call a method in Objective-C is to send a message, but what about sending an undefined message to an instance object? The result was crash. In fact, the intermediate system gave us a second chance to forward the message

If not transferred to the definition of news, the runtime will give it a second chance, first call methodSignatureForSelector or go to the method signature, and then call forwardInvocation, if user defined classes, not rewrite the two methods, namely do not support the way forward

  • @interface LOCBird : NSObject { NSString* name_; } @end @implementation LOCBird - (id)init { self = [super init]; if (self) { name_ = [[NSString alloc] initWithString:@"I am a Bird!!"] ; } return self; } - (void)dealloc { [name_ release]; [super dealloc]; } - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature* signature = [super methodSignatureForSelector:aSelector]; if (signature==nil) { signature = [name_ methodSignatureForSelector:aSelector]; } NSUInteger argCount = [signature numberOfArguments]; for (NSInteger i=0 ; iNSLog(@"%s" , [signature getArgumentTypeAtIndex:i]); } NSLog(@"returnType:%s ,returnLen:%d" , [signature methodReturnType] , [signature methodReturnLength]); NSLog(@"signature:%@" , signature); return signature; } - (void)forwardInvocation:(NSInvocation *)anInvocation { NSLog(@"forwardInvocation:%@" , anInvocation); SEL seletor = [anInvocation selector]; if ([name_ respondsToSelector:seletor]) { [anInvocation invokeWithTarget:name_]; } } @endCopy the code