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.


转载于:https://www.cnblogs.com/Paralleluniverses/archive/2012/02/14/2347686.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值