[Dark Horse programmer] OBJECTIVE-C multi-State of object-oriented three characteristics

Source: Internet
Author: User

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

Related Article

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.