Objective-c polymorphism: Dynamic type recognition + dynamic binding + dynamic loading

Source: Internet
Author: User

One, Objective-c polymorphic

1. Concept: Same interface, different implementations

From different classes you can define methods that share the same name.

Dynamic typing enables the program to determine the type of the object until it is executed

Dynamic type binding allows a program to determine the actual method to invoke on an object until it executes

Unlike traditional programming languages, 2.objective-c can then be run with new data types and new program modules: Dynamic type recognition, dynamic binding, dynamic loading

3.id type: Generic pointer type, weak type, no type checking at compile time

Second, dynamic type recognition

1. Subclasses of any nsobject inherit the ISA instance variable of NSObject, and when a subclass of NSObject instantiates an object, the ISA instance variable is always the first instance variable of the object.

2. Class objects

* The class object re-run is always present.

* Class object is a data structure that stores basic information about a class: Class size, class name, version of the class, and the mapping table for messages and functions.

* The information held by the class object is determined at the time of program compilation and loaded into memory when the program starts.

* Class objects represent classes, classes represent class objects, class methods belong to class objects

* If the recipient of the message is the class name, the class name represents the class object

* Runtime, all instances of the class are generated by the class object, the class object will modify the value of the instance ISA to its own address, each instance of Isa point to the instance of the Class object, * from the class object can know the parent class information, can respond to the method, etc.

* Class objects can only use class methods, not instance methods

3.SEL type

Objective-c at compile time, according to the name of the method (including the parameter sequence), generates a unique identifier (ID) to distinguish the method, the identifier (ID) is the SEL type, at run time by means of the method to find the method. As long as the name of the method (including the parameter sequence) is the same, their IDs are the same. The method can be marked with the @select () indicator. SEL Mydraw [email protected] (draw);

Nsselectorfromstring (nsstring*); Get method ID according to method name

(nsstring*) Nsstringfromselector (sel); method name to get SEL type

4. Common methods for dynamic type recognition

-(BOOL) Whether Iskindofclass:classobj is a classobj class or its subclasses

-(BOOL) Whether Ismemberofclass:classobj is an instance of Classobj

-(BOOL) Whether this method is available in the Respondstosselector:selector class

Nsclassfromstring (nsstring*); Get class object by string

Nsstringfromclass (class name Class); Get string by class name

Class rectclass= [Rectangle class];

Class AClass (=[anobject Class);

if ([Obj1 class]== [Obj2 class]) determines if an instance of the same class

5. You can divide an object into ID type and static type

– If you do not involve polymorphism, use static types as much as possible

– Static types can better indicate errors at compile-time rather than at run-time

– Static types can improve program readability

Third, dynamic binding

1. In Objective-c, the invocation of a specified method within an object is not determined by the compiler but is determined by the runtime, which is called the dynamic binding of the method.

2. In Objective-c, the object does not call the method, but instead receives the message, the message expression is: [Reciver message]; The runtime system first determines the type of receiver (dynamic type recognition) and then selects the dependent method execution based on the message name in the method list of the class, In the source code the message is also called the selector (selector)

3. Function of message functions:

– First the receiver of the first parameter is used to find its ISA pointer, and then the second parameter selector the lookup method in the class object pointed to by ISA;

– If not found, use the new Isa pointer in the current class object to find it in the class object of the parent class at the top level;

– When the method is found, the current object is found based on the self pointer in receiver, invokes the actual implementation method (IMP) of the current object, and then passes the parameter to invoke the implementation method.

– If you have been able to find the class object for NSObject and have not found the method you called, you will report an error that does not recognize the sending message.

4. Method structure in Objetive-c

struct objc_method{

SEL method_name;//Method Name

Char *method_types; Method Address

IMP Method_imp; Method Address (IMP)

};

Typedefobjc_method method;

5. What is imp

–IMP is the abbreviation for "Implementation", which is the address of the Objetive-c method implementation code block, similar to a function pointer, through which you can directly access any method. Eliminate the cost of sending messages.

6. Imp for obtaining a method

–-(IMP) Methodforselector: (SEL) Aselector;

Sel Print_sel =nsselectorfromstring (@ "Print:");//Get sel imp imp=[person methodforselector:print_sel];//get IMP imp ( person,print_sel,@ "*********");//Direct call method by IMP equivalent call: [Person print_sel:@ "*********"];

The first parameter of the –IMP is the object itself (self), the second parameter is the method label, and the third is the parameter of the method

Iv. Dynamic Loading: Loading new classes at runtime

Creating a new class at run time requires only 3 steps:

1. Allocate storage space for class pair, use Objc_allocateclasspair function

2, increase the need to use class_addmethod function, increase the instance variable with Class_addivar

3. Register this class with the Objc_registerclasspair function so that it can be used by others.

Note: Use these functions to draw #import <objc/runtime.h>

Objective-c polymorphism: Dynamic type recognition + dynamic binding + dynamic loading

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.