Dark Horse Programmer------OC Object-oriented polymorphism

Source: Internet
Author: User

-----iOS training, Android training, Java training, and look forward to communicating with you-----

I. The basic concept of polymorphism

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, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it (excerpt from the "DELPHI4 Programming Technology Insider" )。 To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. Polymorphism is implemented in Object Pascal and C + + through virtual functions (virtual function), and there is no more inheritance in OC than the concept of virtual function, and the polymorphism in OC is embodied by simple inheritance.

Simply put: An object corresponds to a different type.

Polymorphism is reflected in the code:

The reference to the parent class or interface to the object of its child class.

Benefits of Polymorphism:
Increases the extensibility of the code, which can be used later by the code defined in the previous section.

Disadvantages of polymorphism:
Pre-defined content cannot use (invoke) the specific content of the late sub-class.

Prerequisites for polymorphism:
1, must have relations, inheritance, implementation.
2, to have coverage.

Ii. Polymorphism in Code demo

First of all, we first create a child parent class inheritance relationship, parent class: Animal, Subclass: Dog, Cat;

Animal class:

Animal @interface animal:nsobject-(void) eat; @end @implementation animal-(void) eat{    NSLog (@ "animal-eats----");} @end

Dog class:

Dog @interface dog:animal-(void) run, @end @implementation  dog-(void) run{    NSLog (@ "Dog---run up");} -(void) eat{    NSLog (@ "dog-eats something----");} @end

Cat class:

Cat @interface cat:animal@end@implementation cat-(void) eat{    NSLog (@ "cat-eats----");} @end

Test procedure:

#import <foundation/foundation.h>//This function is designed to feed animals, dogs eat things//void feed (dog *d)//{//[D eat];//}////void feed2 (Cat *c )//Cat eats//{//[C eat];//}////If the parameter uses a parent class type, you can pass in the parent class, the subclass object void feed (Animal *a) {[A eat];}    int main () {//NSString *d = [Cat new];        [D eat];    /* Animal *AA = [Dog new];        Polymorphism limitations: A variable of the parent class type cannot be used to call a subclass method//[aa run]; Convert AA to Dog * type of variable dog *DD = (dog *) AA;    Dog is [DD run];    */* Animal *AA = [Animal new]; Feed (AA);    Call animal Eating method dog *DD = [dog new]; Feed (DD);    How to call the dog to eat cat *cc = [cat new]; Feed (CC); Call the cat's Eating method */*//NSString *s = [Cat new];            Cat non-NSString subclass, non-reference Animal *c = [Cat new]; NSObject *n = [Dog new];            NSObject is the parent class of all OC objects and can be referenced nsobject *n2 = [Animal new]; Multiple morphology//dog *d = [Dog new];        Dog type//polymorphic: The parent pointer points to the subclass object Animal *a = [Dog new];    The real image of the object is detected when the method is called [a eat]; */return 0;}

Note the point:

If we want to call the cat or dog "eat" method, we can write the following code:

This function is specifically used to feed animals, dogs eat void feed (dog *d)   {    [D eat];} void Feed2 (cat *c)//Cat eats {    [C eat];}

But this code is not elegant, it can be very bad, we know from the above code that there is a-(void) Eat method in the dog class, the Cat class has a-(void) Eat method, but the Eat method in this two class is extracted from the animal class, if we change the form parameter, to simplify the code , this is where polymorphism comes in.

If the parameter uses a parent class type, you can pass in the parent class, subclass object void Feed (Animal *a) {    [a eat];}

Of course, this may seem similar here, but let's just imagine that if a function in a project is not as simple as using polymorphic means in large projects, then the amount of code will be reduced a lot, and there will be a lot of useless redundant code, and the code looks elegant ~

Iii. Summary

1. There is no polymorphism without inheritance
2. Code embodiment: Pointer to the parent class pointer to child class object
3. Benefits: If the function \ Method parameter is used in the parent class type, you can pass in the parent class, the child class object
4. Limitations:
1> a variable of a parent class type cannot directly invoke a method that is unique to a subclass. You must strongly convert to a subclass type variable to directly invoke a method that is specific to a subclass class

Such as:

Animal *AA = [Dog new];    Polymorphism limitations: A variable of the parent class type cannot be used to call a subclass method    //[aa run];        Variable AA to Dog * Type    dog *DD = (dog *) AA;//dog is        [DD run];

Dark Horse Programmer------OC Object-oriented polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.