objective-C 2.0:polymorphism, dynamic binding and dynamic
polymorphism, dynamic binding and dynamic
Dynamic Binding and the id Type
①When combined with polymorphism, dynamic binding and dynamic typing enable you to easily write code that can send the same message to objects from differentclasses.
②The real power of thisdata type is exploited when it’s used this way to store different types of objects in a variable during the execution of a program.
③dynamic typing and dynamic binding—that is, the system makes the decision about the class of the object, and, therefore, which method to invoke dynamically, at runtime instead of at compile time.
Argument and Return Types with Dynamic Typing
①If a method with the same name is implemented in more than one of your classes, each method must agree on the type of each argument and the type of value it returns so that the compiler can generate the correct code for your message expressions.
②At runtime, the Objective-C runtime system will check the actual class of the object stored inside dataValue1 and select the appropriate method from the correct class to execute. However, in a more general case, the compiler might generate the incorrect code to pass arguments to a method or handle its return value.This would happen if one method took an object as its argument and the other took a floating-point value, for example. Or Asking Questions About Classes 187 if one method returned an object and the other returned an integer, for example. If the inconsistency between two methods is just a different type of object (for example, the Fraction’s add: method takes a Fraction object as its argument and returns one, and the Complex’s add: method takes and returns a Complex object), the compiler will still generate the correct code because memory addresses (that is, pointers) are passed as references to objects anyway.
Asking Questions About Classes
①Table 9.1 Methods for Working with Dynamic Types Method Question
②To generate one of the so-called selectors listed inTable 9.1, you apply the @selector
directive to a method name. For example, the following produces a value of type SEL for
the method named alloc, which you know is a method inherited from the NSObject class:
@selector (alloc)
The following expression produces a selector for the setTo:over: method that you implemented
in your Fraction class (remember those colon characters in the method names):
@selector (setTo:over:)
To see whether an instance of the Fraction class responds to the setTo:over: method, you can test the return value from the expression, like this:
[Fraction instancesRespondToSelector: @selector (setTo:over:)]
Remember, the test covers inherited methods, not just one that is directly defined in the class definition.
③The respondsToSelector: method is used extensively in iOS for implementing the concept
of delegation. As you’ll learn in Chapter 10, “More on Variables and Data Types,” the
system will often give you the option to implement one or more methods in your class to
handle certain events or provide certain information (such as the number of sections in a
table). In order for the system to determine whether you have in fact implemented a particular
method, it will use respondsToSelector: to see if it can delegate the handling of the
event to your method. If you didn’t implement the method, it will take care the event itself,
using whatever is defined as the default behavior.