Qf--oc polymorphism, dynamic binding and implementation principle

Source: Internet
Author: User

Polymorphic:

Encapsulation, inheritance, polymorphism are the three main characteristics of object-oriented.

What exactly is that polymorphic?

Polymorphism: Allows different classes to define the same method, OC can judge by himself, not chaos.

Dynamic type: The program does not know the class to which the object belongs until run time.

Dynamic binding: Based on the dynamic type, the program will not determine the method to execute until run time.

Polymorphic: A pointer to the parent class can point to the object of the subclass. This sentence must be deeply understood, please look at a few lines of code:

Animal * obj = [[Dog alloc] init];

[obj eat];

Although the obj is preceded by the animal class, that is, it is declared to be a pointer to the animal class, but runs [obj eat], which in fact is the object that points to the subclass dog. It is also said that when it is running, it actually calls the Eat method in the dog class. This is polymorphism. In a nutshell, although obj is declared to be of type animal, obj is defined as the dog type, and it is actually the type that invokes the overridden method in its own class.

The principle of dynamic capability in OC:

  Each object in OC has a property, the Isa pointer , by default. It points to a "class object" (Object-class), which is generated by the compiler at compile time. It stores information about classes that are specifically used to describe classes. When the program runs, it acts as a property of an instance of the class, and it dynamically tells the object "which class do you belong to, what methods you have" ...

Polymorphism can improve the flexibility and reusability of code.

Blog reference: Click to enter

Qf--oc polymorphism, dynamic binding and implementation principle

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.