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

在面向對象的編程中最重要的概念之一是繼承。繼承允許我們定義一個類在另一個類,這使得它更容易創(chuàng)建和維護應用程序方面。這也提供了一個機會,重用代碼的功能和快速的執(zhí)行時間。

當創(chuàng)建一個類,而不是寫入新的數(shù)據(jù)成員和成員函數(shù),程序員可以指定新的類繼承現(xiàn)有類的成員。這種現(xiàn)有的類稱為基類,新類稱為派生類。

繼承的想法實現(xiàn)是有關系的。例如,哺乳動物是一個動物,狗是哺乳動物,因此狗是動物等。

基類和派生類:

Objective-C中允許多重繼承,也就是說,它只能有一個基類,但允許多層次的繼承。Objective-C中的所有類的超類為NSObject。

@interface derived-class: base-class

考慮一個基類 Shape 和它的派生類Rectangle 如下:

#import <Foundation/Foundation.h>
 
@interface Person : NSObject

{
    NSString *personName;
    NSInteger personAge;
}

- (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end

@implementation Person

- (id)initWithName:(NSString *)name andAge:(NSInteger)age{
    personName = name;
    personAge = age;
    return self;
}

- (void)print{
    NSLog(@"Name: %@", personName);
    NSLog(@"Age: %ld", personAge);
}

@end

@interface Employee : Person

{
    NSString *employeeEducation;
}

- (id)initWithName:(NSString *)name andAge:(NSInteger)age 
  andEducation:(NSString *)education;
- (void)print;

@end


@implementation Employee

- (id)initWithName:(NSString *)name andAge:(NSInteger)age 
  andEducation: (NSString *)education
  {
    personName = name;
    personAge = age;
    employeeEducation = education;
    return self;
}

- (void)print
{
    NSLog(@"Name: %@", personName);
    NSLog(@"Age: %ld", personAge);
    NSLog(@"Education: %@", employeeEducation);
}

@end


int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];        
    NSLog(@"Base class Person Object");
    Person *person = [[Person alloc]initWithName:@"Raj" andAge:5];
    [person print];
    NSLog(@"Inherited Class Employee Object");
    Employee *employee = [[Employee alloc]initWithName:@"Raj" 
    andAge:5 andEducation:@"MBA"];
    [employee print];        
    [pool drain];
    return 0;
}

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

2013-09-22 21:20:09.842 Inheritance[349:303] Base class Person Object
2013-09-22