What is polymorphic?
Polymorphism (polymorphism) literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. The description of polymorphism is referenced by Charlie Calverts-polymorphism is a technique that allows you to set a parent object to be equal to one or more of his sub-objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply: a pointer to the parent class type is allowed to assign a pointer to the child class type.
Simple example
#import<Foundation/Foundation.h>@interfaceAnimal:nsobject-(void) eat;@end@implementationAnimal-(void) eat{NSLog (@"Animal eating ...");}@end@interfaceDog:animal@end@implementationDog-(void) eat{NSLog (@"Dog eating ...");}@endintMain () {//Call with the dog class pointerDog *dog = [dogNew]; [Dog Eat]; //called using the animal type pointerAnimal *animal = [DogNew]; [Animal eat]; return 0;}
Both call methods call the Eat method in the dog class, not the second call to the Eat method in the animal class.
The benefits of polymorphism
Consider a requirement, when we need a way to perform the feeding of the dog (this example is actually not very good, but the actual demand may be worse than this, so suppose it together).
We'll have to
[Dark Horse programmer]objective-c The polymorphism of object-oriented three features