1. Concept of Polymorphism
The premise of polymorphism, there must be an inheritance relationship, in the form of code is the parent class type to save the subclass type, that is, the parent class Pointer Points to the subclass object.
Ii. Polymorphism
In oC, the call method is to detect the real type of the object, which is called dynamic binding.
The parent class saves the subclass pointer. To call a method, perform the following steps:
1) during compilation, the method called by the pointer of the parent class pointing to the subclass is checked, and whether the method exists in the parent class. If yes, the method is compiled through
2) at runtime, the actual type of the initial object will be dynamically checked
Iii. Usage of Polymorphism
Provides dynamic binding to reduce unnecessary program redundancy. In a method, the parent class is used as a parameter so that the method can call all subclasses of the same method.
Iv. Benefits of Polymorphism
The emergence of polymorphism improves program scalability and maintenance later.
5. Limitations of Polymorphism
If a subclass contains a method not available in the parent class, force type conversion is required when the method is called through the parent class pointer. Otherwise, an error is reported.
Strong type conversion method:
Cat * cat = (cat *) animal;
6. Code
Animal: Animal has the Eat Method
Subclass: cat class Dog class, all inherit the Eat method of the parent class, and load
Breeders: breeder has the cat dog feeding method.
1. Animal parent class
===Animal.h===@interface Animal : NSObject-(void)eat;@end
2. Dog Cat subclass
===Cat===@interface Cat : Animal@end@implementation Cat-(void)eat{ NSLog(@"Cat eat ..");}@end===Dog===@interface Dog : Animal@end@implementation Dog-(void)eat{ NSLog(@"Dog eat ...");}@end
3. Breeders
=== Breeder. h ===@ interface breeder: nsobject // non-polymorphism-(void) breedcat :( cat *) CAT;-(void) breeddog :( dog *) dog; // polymorphism-(void) breed :( animal *) animal; @ end = breeder. M ===@ implementation breeder // raises cat-(void) breedcat :( cat *) Cat {[cat eat];} // raises dog-(void) breeddog :( dog *) dog {[Dog eat];}/* multi-state call: treats the parent class as a parameter, so that this method has the ability to call all sub-classes to the same method for Polymorphism: reduced redundancy, instead of writing a Method */-(void) breed :( animal *) animal {[animal eat];} @ end
4. Main Function
Int main (INT argc, const char * argv []) {@ autoreleasepool {// simulate a breeder breeding animal breeder * breeder = [[breeder alloc] init]; nslog (@ "======= non-polymorphism call ======"); // you need to write a method for each animal, cat * cat = [[Cat alloc] init]; [breeder breedcat: Cat]; dog * dog = [[DOG alloc] init]; [breeder breeddog: Dog]; nslog (@ "====== polymorphism call ===="); // no more specific method is provided for each animal, reduce redundancy // declare the parent class to receive the Sub-class pointer animal * c = [[Cat alloc] init]; animal * D = [[DOG alloc] init]; [breeder breed: c]; [breeder breed: d]; // You can also use the subclass as the parameter [breeder breed: Cat]; [breeder breed: Dog];} return 0;} output: 22:58:13. 788 inheritance and polymorphism [2938: 303] ====== non-polymorphism call ====== 22:58:13. 789 inheritance and polymorphism [2938: 303] cat eat .. 22:58:13. 790 inheritance and polymorphism [2938: 303] Dog eat... 22:58:13. 790 inheritance and polymorphism [2938: 303] =======polymorphism call ====== 22:58:13. 790 inheritance and polymorphism [2938: 303] cat eat .. 22:58:13. 790 inheritance and polymorphism [2938: 303] Dog eat... 22:58:13. 791 inheritance and polymorphism [2938: 303] cat eat .. 22:58:13. 791 inheritance and polymorphism [2938: 303] Dog eat...
OC Study Notes 12 Polymorphism