Dark Horse programmer -15-oc polymorphic

Source: Internet
Author: User


I. polymorphic Concepts

U parent class pointer to subclass object (conditional, must have inheritance)

U Limitations: Parent-class pointers cannot call methods of subclasses

U Benefits: If we use the parent class type in this function \ method, we can pass in objects such as the parent class

Ii. Basic use of polymorphism

#import <Foundation/Foundation.h>

Animal's statement

@interface Animal:nsobject

Defining a Eat Object method

-(void) run;

@end

@implementation Animal

-(void) run

{

NSLog (@ "Animals run two laps");

}

@end

The dog class, inherited the animal

@interface Dog:animal

-(void) run;

@end

@implementation Dog

-(void) run

{

NSLog (@ "Dog running two laps");

}

@end

int main ()

{

Creating a subclass Dog Object

Dog *d = [Dognew];

[D run];

The pointer to the parent class here points to the object of the child class

Animal *a =[dog New];

[a run];

Create a parent Class animal object

Animal *a1 =[animal New];

[A1 run];

}

Output: Dog running two laps

The dog runs two laps

Animals run two laps

It is also possible for the parent class to invoke the object method of the subclass in the example above, that is, the parent pointer points to the child class object

Iii. Summary of polymorphism

U 1) No inheritance, no polymorphism.

U 2) The embodiment of the code: the pointer of the parent class pointer to the child class object

U 3) Benefits: If you are using a parent class type in a function method parameter, you can pass in the parent class and the subclass object without having to define multiple functions to match the corresponding class.

U 4) Limitations: A variable of a parent class type cannot directly invoke a subclass-specific method, and must be cast to a subclass-specific method if it must be called.

Iv. Some considerations for polymorphism

1, OC language is a weak grammar of the language, compile time and will not error, so this requires us in the actual development process must be in accordance with the established norms to write code, do not appear the dog is a string such problems. Such as:

Dog *d=[[animal alloc] init]; Animal is a dog? Not semantically correct

NSString *str=[dog New]; Dog is a string? Not correct

2. The benefits of polymorphism:

A new function is needed to feed the dog.

Void Feed (Dog *d)

{

[D eat];

}

If you need to feed the cat at this time, you should rewrite a new function

Void Feed (Cat *c)

{

[C Eat];

}

Analysis: While dogs and cats are actually inherited from the animal class, you can use polymorphism here to simplify the code.

Only the parameters of the function are written in the animal * type, so both the dog and cat type objects can be passed in. You can change the parameters directly when you call them.

3. Polymorphism limitations: Pointer variables of a parent class type cannot directly invoke methods that are specific to subclasses.

Animal *aa=[[dog alloc] init];

[A run];//does not have a run method in the animal class, which invokes the method of the dog object.

You can instead: you can cast a to a variable of type dog*, as follows:

Dog *d= (dog *) a;//uses casts, where a and D point to the same dog object

Dark Horse programmer -15-oc polymorphic

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.