鍍金池/ 教程/ C/ Objective-C 動(dòng)態(tài)綁定
Objective-C 多態(tài)性
Objective-C 預(yù)處理器
Objective-C for循環(huán)
Objective-C 開(kāi)發(fā)環(huán)境(安裝配置)
Obj-C Foundation/基礎(chǔ)框架
Objective-C 指針運(yùn)算
Objective-C 循環(huán)
Objective-C 錯(cuò)誤處理
Objective-C while循環(huán)
Objective-C if語(yǔ)句
Objective-C do...while循環(huán)
Objective-C教程
Objective-C 嵌套switch語(yǔ)句
Objective-C 函數(shù)按值調(diào)用
Objective-C 常量
Objective-C 運(yùn)算符
Objective-C 數(shù)據(jù)類(lèi)型
Objective-C 邏輯運(yùn)算符
Objective-C 數(shù)組作為函數(shù)參數(shù)傳遞
Objective-C struct/結(jié)構(gòu)
Objective-C 嵌套循環(huán)
Objective-C 函數(shù)引用調(diào)用
Objective-C 日志處理
Objective-C 數(shù)據(jù)存儲(chǔ)
Objective-C 教程首頁(yè)
Objective-C 擴(kuò)展
Objective-C 異常處理
Objective-C 類(lèi)型轉(zhuǎn)換
Objective-C 嵌套 if 語(yǔ)句
Objective-C typedef
Objective-C 決策
Objective-C 指針的數(shù)組
Objective-C Protocols/協(xié)議
Objective-C 日期和時(shí)間
Objective-C 指針
Objective-C 內(nèi)存管理
命令行參數(shù)
Objective-C NSString/字符串
Objective-C 動(dòng)態(tài)綁定
Objective-C 復(fù)合對(duì)象
Objective-C Arrays/數(shù)組
Objective-C 位運(yùn)算符
Objective-C 指向指針的指針
Objective-C 從函數(shù)返回?cái)?shù)組
Objective-C Posing/冒充
Objective-C Categories/類(lèi)別
Objective-C 文件處理
Objective-C 賦值運(yùn)算符
Objective-C 函數(shù)返回指針
Objective-C if...else 語(yǔ)句
Objective-C switch語(yǔ)句
Objective-C URL加載系統(tǒng)
Objective-C 算術(shù)運(yùn)算符
Objective-C Numbers/數(shù)字
Objective-C語(yǔ)言程序結(jié)構(gòu)
Objective-C 快速枚舉
Objective-C 基本語(yǔ)法
Objective-C 類(lèi)&對(duì)象
Objective-C 變量
Objective-C 關(guān)系運(yùn)算符
Objective-C 塊
Objective-C break語(yǔ)句
Objective-C continue語(yǔ)句
Objective-C 語(yǔ)言概述
Objective C 文本和字符串
Objective-C 函數(shù)
Objective-C 傳遞函數(shù)的指針
Objective-C 數(shù)組的指針
Objective-C 多維數(shù)組
Objective-C 繼承
Objective-C 數(shù)據(jù)封裝

Objective-C 動(dòng)態(tài)綁定

動(dòng)態(tài)綁定在運(yùn)行時(shí)要調(diào)用的方法,而不是在編譯時(shí)確定。也被稱(chēng)為動(dòng)態(tài)綁定后期綁定。

Objective-C中,所有的方法都解決了在運(yùn)行時(shí)動(dòng)態(tài)。是由方法名稱(chēng)(選擇器)和接收消息的對(duì)象所執(zhí)行的確切的代碼。

動(dòng)態(tài)綁定能夠多態(tài)性。例如,考慮的對(duì)象,包括 Rectangle 和Square集合。每個(gè)對(duì)象都有自己實(shí)現(xiàn)printArea 方法。

在下面的代碼片段,表達(dá)應(yīng)執(zhí)行的實(shí)際代碼 [anObject printArea]  在運(yùn)行時(shí)確定。運(yùn)行系統(tǒng)使用選擇運(yùn)行的方法,以確定適當(dāng)?shù)姆椒ㄔ谌魏晤?lèi)對(duì)象。

讓我們來(lái)看看一個(gè)簡(jiǎn)單的代碼,這可以解釋動(dòng)態(tài)綁定。

#import <Foundation/Foundation.h>

@interface Square:NSObject
{
   float area;
}
- (void)calculateAreaOfSide:(CGFloat)side;
- (void)printArea;
@end

@implementation Square

- (void)calculateAreaOfSide:(CGFloat)side
{
    area = side * side;
}
- (void)printArea
{
    NSLog(@"The area of square is %f",area);
}

@end

@interface Rectangle:NSObject
{
   float area;
}
- (void)calculateAreaOfLength:(CGFloat)length andBreadth:(CGFloat)breadth;
- (void)printArea;
@end

@implementation  Rectangle

- (void)calculateAreaOfLength:(CGFloat)length andBreadth:(CGFloat)breadth
{
    area = length * breadth;
}
- (void)printArea
{
    NSLog(@"The area of Rectangle is %f",area);
}

@end

int main()
{
   Square *square = [[Square alloc]init];
   [square calculateAreaOfSide:10.0];
   Rectangle *rectangle = [[Rectangle alloc]init];
   [rectangle calculateAreaOfLength:10.0 andBreadth:5.0];
   NSArray *shapes = [[NSArray alloc]initWithObjects: square, rectangle,nil];
   id object1 = [shapes objectAtIndex:0];
   [object1 printArea];
   id object2 = [shapes objectAtIndex:1];
   [object2 printArea];
   return 0;
}

現(xiàn)在,當(dāng)我們編譯并運(yùn)行程序,我們會(huì)得到以下的結(jié)果。

2013-09-28 07:42:29.821 demo[4916] The area of square is 100.000000
2013-09-28 07:42:29.821 demo[4916] The area of Rectangle is 50.000000

正如可以看到在上面的例子中,printArea 方法是在運(yùn)行時(shí)動(dòng)態(tài)選擇。這是一個(gè)動(dòng)態(tài)綁定的例子,在同類(lèi)對(duì)象打交道時(shí)情況下是非常有用。