IOS Development notes series-basics 3 (polymorphism, dynamic type, and dynamic binding), ios Polymorphism

Source: Internet
Author: User

IOS Development notes series-basics 3 (polymorphism, dynamic type, and dynamic binding), ios Polymorphism

Polymorphism: the same name and different classes

Make the ability of different classes to share the same method name become polymorphism. It allows you to develop a group of classes. Each class in this group can respond to the same method name. Each class definition encapsulates the Code required to respond to a specific method, so that it is independent from other class definitions. This is because the system knows the Class Object of the Message Receiver when executing the method during Objective-C runtime. It always carries information such as "which class is an object, this information allows the system to make decisions at runtime, rather than during compilation.

 

Dynamic binding and id type

The id data type is a common object type that can be used to store objects of any class. When the id type is used, some tests of the program are postponed from compilation to runtime, because the system cannot determine the type of the Data Type during compilation.

If you use a dynamic type to call a method, pay attention to the rule: if the method with the same name is implemented in multiple classes, each method must meet the type and return value type of each parameter, in this way, the compiler can generate the correct code for the message expression.

All classes in Objective-C are inherited from the NSObject class, which supports some methods to avoid errors or check program integrity during program running, as shown below:

Method Problem or behavior
-(BOOL) isKindOfClass: class-object Is an object a member of a class-object or its subclass?
-(BOOL) isMemberOfClass: class-object Is the object a member of class-object?
-(BOOL) respondsToSelector: Selector Whether the object can respond to the method specified by selector
-(BOOL) instancesRespondToSelector: Selector Whether the specified class instance can respond to selector
-(BOOL) isSubclassOfClass: class-object Whether the object is a subclass of the specified class
-(BOOL) implements mselector: Selector Apply the method specified by selector
-(BOOL) specify mselector: Selector withObject: object Apply the method specified by selector to pass the object parameter
-(BOOL) specify mselector: Selector withObject: object1 withObject: object2 Apply the method specified by selector to pass the object1 and object2 parameters.

 

  

 

 

 

 

 

 

In this way, before calling a method on an id object, you can first make some judgments to prevent possible errors and crashes.

In addition, you can also use other policies, such as using forwardInvocation: to forward messages to other objects for processing.

In actual development, you can also use the Protocol to make some constraints on the id type, so that the compiler can know in advance whether the method call is correct.

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.