Resolve polymorphism, dynamic type, and dynamic binding in Objective-C

Source: Internet
Author: User

Objective-CMediumPolymorphism, DynamicState typeAndDynamicBinding is the content to be introduced in this article.Objective-CTo learnObjective-CFor more information, see the detailed description.

I. Polymorphism

The same name and different classes. Share the same method name with different classes.

Ii. dynamic binding and id type

Id -- generic object type, which can be used to store objects belonging to any class. For example, id dataValue; no need to add)

Iii. Check during compilation and Runtime

The object type stored in the id variable cannot be determined during compilation, so some tests are postponed until runtime.

Iv. id data type and static type

When a variable is defined as an object of a specific class, the static form is used.

Call a method using a dynamic type. Note: if the method with the same name is implemented in multiple classes, each method must conform to the type of each parameter and the type of return value.

V. Related Issues

Some basic methods supported by the NSObject class.

 
 
  1. -(BOOL) isKindOf: class-object determines whether the object is a class-object or a member of its subclass)
  2. -(BOOL) isMenberOfClass: class-object determines whether the object is a member of class-object)
  3. -(BOOL) respondsToSelector: selector determines whether the object can respond to the method specified by selector)
  4. + (BOOL) instancesRespondToSelector: selector determines whether the specified class instance can respond to the method specified by selector)
  5. + (BOOL) isSubclassOfClass: class-object determines whether the object is a subclass of the specified class)
  6. -(Id) specify mselector: selector applies the method specified by selector)
  7. -(Id) specify mselector: selector withObject: the object applies the method specified by selector, passing the object parameter)
  8. -(Id) specify mselector: selector withObject: object1 withObject: apply the method specified by selector to object2 and pass the parameters object1 and object2)

To generate a class object based on the class name or another object, you can send a class message to it. For example, to obtain class objects from a class named myClass, you can write: [myClass class];

6. Use @ try to handle exceptions

@ Try format:

 
 
  1.      
  2.  @try{  
  3.  statement  
  4.  statement  
  5.  ……  
  6. }  
  7.  @catch(NSException *exception){  
  8.  statement  
  9.  statement  
  10.  ……  

After the statement is added to the @ try block, the program runs normally. However, if an exception is thrown by a statement in the block, the execution will not be terminated, but will jump to the catch Block immediately to continue the execution. An executable program that can handle exceptions within the @ catch Block records error messages, and clearly and terminates the execution.

@ Finally the block contains the @ try BLOCK statement code that indicates whether to execute and throw an exception.

The @ throw command allows you to throw your own exceptions. You can use this command to throw a specific exception or throw an exception in the @ catch Block that takes you into code similar to the following: # throw;

Summary: AnalysisObjective-CMediumPolymorphism,Dynamic typeAndDynamicThe bound content has been introduced. I hope this article will help you.

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.