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.
- -(BOOL) isKindOf: class-object determines whether the object is a class-object or a member of its subclass)
- -(BOOL) isMenberOfClass: class-object determines whether the object is a member of class-object)
- -(BOOL) respondsToSelector: selector determines whether the object can respond to the method specified by selector)
- + (BOOL) instancesRespondToSelector: selector determines whether the specified class instance can respond to the method specified by selector)
- + (BOOL) isSubclassOfClass: class-object determines whether the object is a subclass of the specified class)
- -(Id) specify mselector: selector applies the method specified by selector)
- -(Id) specify mselector: selector withObject: the object applies the method specified by selector, passing the object parameter)
- -(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:
-
- @try{
- statement
- statement
- ……
- }
- @catch(NSException *exception){
- statement
- statement
- ……
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.