1, Basic introduction
Polymorphism for the object-oriented thinking, the personal feeling is really important, he will be writing code in the elegant way also plays a very important role, in fact, many of the design patterns are used in most of the characteristics of polymorphic, Java in the polymorphic features used is very convenient, but C + + is very difficult to use, In fact, many states are: the definition of the type and the actual type, is generally based on the form of interface implementation, not to mention, directly see examples:
Examples of printers
Abstract Printer Class Printer
Printer.h
#import @interface Printer:nsobject -(void) print; @end
is a simple way to print
Printer.m
#import " Printer.h " @implementation Printer -(void) print{ NSLog (@ " printer print sheet "); } @end
The implementation is also very simple
Here's a look at the specific subclass
ColorPrinter.h
#import " Printer.h " // to modify the print behavior of a parent class @interface Colorprinter:printer -(void) print; @end
Colorprinter.m
#import " ColorPrinter.h " @implementation Colorprinter -(void) print{ NSLog (@ " Color Printer "); } @end
Take a look at another subclass
BlackPrinter.h
#import " BlackPrinter.h " @implementation Blackprinter -(void) print{ NSLog (@ " black and white printer "); } @end
Here we are defining a person class that is used to manipulate the specific printer
Person.h
1 #import "Person.h" 2 3 @implementation Person4 5 /* 6 -(void) Printwithcolor: (Colorprinter *) colorprint{7 [colorprint print];8 } 9 Ten -(void) Printwithblack: (Blackprinter *) blackprint{ One [blackprint print]; A } - */ - the- (void) Doprint: (Printer *) printer{ - [printer Print]; - } - + @end
Let's take a look at the test code:
Main.m
1 #import 2 3 #import "Person.h" 4 #import "BlackPrinter.h" 5 #import "ColorPrinter.h" 6 7 intMainintargcConstCharchar *argv[]) { 8 @autoreleasepool {9 TenPerson *person =[[Person alloc] init]; One AColorprinter *colorprint =[[Colorprinter alloc] init]; -Blackprinter *blackprint =[[Blackprinter alloc] init]; - the //the definition of polymorphism - /* - Printer *p1 = [[Colorprinter alloc] init]; - Printer *p2 = [[Blackprinter alloc] init]; + - [Person DOPRINT:P1]; + [Person DOPRINT:P2]; A */ at - //control which printer to use with commands entered by the console - intcmd; - Do{ -scanf"%d",&cmd); - if(cmd = =1){ in [Person Doprint:colorprint]; -}Else if(cmd = =2){ to [Person Doprint:blackprint]; + } -} while(1); the * } $ return 0; Panax Notoginseng}
View Code
Here is a detailed explanation of the benefits of polymorphism
The above example is a color printer and a black and white printer both printers, and then the person class has a method of printing, of course, this method requires a Printer object, if not implemented by the polymorphic mechanism (Person.h in the code part of the comment), is to define a separate operation for the two printers, and then use the specific Printer object in the PERSON.M (the part of the comment in the code), and in the main.m file, we see that the specified method is called when the person needs to use which printer:
[Person printwithblack:blackprint];//call black-and-white printer [person printwithcolor:colorprint];//call Color Printer
This design is not good, why? If there is another printer now, then we need to define a way to operate the printer in Person.h, so what if we add a new printer later? Are you still adding a method? Then the Person.h file becomes bloated. So this time polymorphism is good, using the parent class type, define a method in Person.h:
-(void) Doprint: (Printer *) Printer;
Here we see that the parameter type of this method is the type of the parent class, which is polymorphic, the definition type is the parent class type, the actual type is the subclass type
-(void) Doprint: (Printer *) printer{ [Printer print]; }
The Print method is called here, which is the actual type of print method passed in.
Printer *P1 = [[Colorprinter alloc] init]; *P2 = [[Blackprinter alloc] init]; [Person DOPRINT:P1]; [Person DOPRINT:P2];
The type on the P1,P2 surface here is printer, but the actual type is the subclass type, so it calls their own corresponding print method.
From the above example, we can see that the polymorphism of the new is very important, of course, it is more difficult to understand the three characteristics.
2. Other knowledge points of polymorphism
1. The manifestation of polymorphism
Person *p = [Student new];
P->age = 100;
[P walk];
Child class object assigned to parent class pointer
Parent-class pointers access corresponding properties and methods
2. The benefits of polymorphism
Receive parameters with parent class, save code
3. Limitations of polymorphism
Cannot access the properties of a subclass (you can consider casting)
4. Polymorphic Details
Dynamic binding: Determines the method of dynamic invocation at run time based on the type of object
The three characteristics of OC--polymorphism