polymorphism
Polymorphism has always been an important idea of object – oriented programming. OC programming is also unavoidable topics. Writing your own code often makes you feel like you suddenly understand these defining things. Record the understanding of these well-defined features each time you work on them. Let’s get down to business. Polymorphism, as the name implies, has multiple states. To be clear, my name is A, but I’m not necessarily A. I might as well be B. This is also based on the fact that OC is A dynamic language. Determined at compile time. Polymorphism in OC is mainly inherited.
Create a MultiPerson class that feeds an animal.
@interface MultiPerson : NSObject
- (void) feedAnimalFood:(MultiAnimal *)animal;
@end
#import "MultiPerson.h"
@implementation MultiPerson
- (void)feedAnimalFood:(MultiAnimal *)animal {
[animal eatFood];
}
@end
Copy the code
Creating an animal class
@interface MultiAnimal : NSObject
- (void)eatFood;
@end
#import "MultiAnimal.h"
@implementation MultiAnimal
-(void)eatFood {}
@end
Copy the code
Create cat and dog classes
//: cat #import "multianimal. h" @interface MultiCat: MultiAnimal @end #import "multicat. h" @implementation MultiCat - (void)eatFood {NSLog(@" feed cat food "); } @end // @interface MultiDog: MultiAnimal @end #import "MultiDog. H "@implementation MultiDog - (void)eatFood {NSLog(@" feed dog to eat dog "); } @endCopy the code
People feeding animals:
//: MultiPerson *p = [[MultiPerson alloc]init]; //: cat = [[MultiCat alloc]init]; //: MultiDog *dog = [[MultiDog alloc]init]; [p feedAnimalFood:cat]; [p feedAnimalFood:dog];Copy the code
Person accepts the animal type but is actually passed either a cat or a dog. That’s polymorphism.
Multiple inheritance
Multiple inheritance means you can have more than one parent class. However, in OC, only single inheritance can be implemented. But we can implement multiple inheritance through other paths.
- The first type: protocol, which I won’t talk about, is to follow a proxy to execute a class’s methods.
- The second type: message forwarding Message forwarding I have also written about the iOS Runtime message forwarding mechanism and its practical use in previous articles
This means that we can forward the current message to a class at runtime through message forwarding. To reach the point where a class executes methods that execute multiple classes.
- NSProxy is not an inheritance of NSProject, and can be disguised as any object in the process of running. It’s actually message forwarding
SmallCat *cat = [[SmallCat alloc]init]; SmallDog *dog = [[SmallDog alloc]init]; SXProxy *proxy = [SXProxy alloc]; [proxy transformObjc:cat]; [proxy selector :@selector(eat: withObject:@" The cat is hungry "]; [proxy transformObjc:dog]; [proxy performSelector:@selector(shut)];Copy the code
Internal implementation:
@interface SXProxy : NSProxy - (void)transformObjc:(NSObject *)objc; @end #import "SXProxy.h" @interface SXProxy () /** * */ @property (nonatomic, strong) NSObject *objc; @implementation SXProxy - (void)transformObjc:(NSObject *)objc{//: copy object self.objc = objc; } #pragma mark -(void)forwardInvocation:(NSInvocation *) Invocation {if (self.objc) {//: Intercepts the invocation Invocation setTarget:self.objc]; If ([self.objc isKindOfClass:[NSClassFromString(@"Cat") class]]) {NSString * STR = @" Message "; [invocation setArgument:&str atIndex:2]; } //: Invocation invoke; }} # pragma mark - 1 query the signature of the method - (NSMethodSignature *) methodSignatureForSelector: (SEL) SEL {NSMethodSignature * signature = nil; if ([self.objc methodSignatureForSelector:sel]) { signature = [self.objc methodSignatureForSelector:sel]; }else{ signature = [super methodSignatureForSelector:sel]; } return signature; } @endCopy the code
Ok, today first write this!!