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

Objective-C 多態(tài)性

多態(tài)是指具有多種形式。通常情況下,多態(tài)發(fā)生時,有一個類層次結(jié)構(gòu)和繼承關(guān)系。

Objective-C的多態(tài)是指一個成員函數(shù)調(diào)用會導(dǎo)致執(zhí)行不同的功能,根據(jù)調(diào)用函數(shù)的對象的類型。

考慮這個例子中,我們有一類形狀,提供了基本的接口,為所有的形狀。Square 和Rectangle 來自基類Shape。

以下方法printArea是要顯示 OOP 多態(tài)性特點(diǎn)。

#import <Foundation/Foundation.h>

@interface Shape : NSObject

{
    CGFloat area;
}

- (void)printArea;

@end

@implementation Shape

- (void)printArea{
    NSLog(@"The area is %f", area);
}

@end


@interface Square : Shape
{
    CGFloat length;
}

- (id)initWithSide:(CGFloat)side;

- (void)calculateArea;

@end

@implementation Square

- (id)initWithSide:(CGFloat)side{
    length = side;
    return self;
}

- (void)calculateArea{
    area = length * length;
}

- (void)printArea{
    NSLog(@"The area of square is %f", area);
}

@end

@interface Rectangle : Shape
{
    CGFloat length;
    CGFloat breadth;
}

- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;


@end

@implementation Rectangle

- (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth{
    length = rLength;
    breadth = rBreadth;
    return self;
}

- (void)calculateArea{
    area = length * breadth;
}

@end


int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Square *square = [[Square alloc]initWithSide:10.0];
    [square calculateArea];
    [square printArea];
    Rectangle *rect = [[Rectangle alloc]
    initWithLength:10.0 andBreadth:5.0];
    [rect calculateArea];
    [rect printArea];        
    [pool drain];
    return 0;
}

上面的代碼編譯和執(zhí)行時,它會產(chǎn)生以下結(jié)果:

2013-09-22 21:21:50.785 Polymorphism[358:303] The area of square is 100.000000
2013-09-22 21:21:50.786 Polymorphism[358:303] The area is 50.000000

printArea方法的基類上是可用,在上面的例子中,無論是在基類中的方法還是執(zhí)行派生類。請注意Objective-C中我們不能訪問父類printArea方法,在這種情況下,方法是在派生類中實(shí)現(xiàn)的。

多態(tài)性處理方法基類和派生類的方法的基礎(chǔ)上實(shí)施的兩個類之間的切換。