- Objective-C基础
- Objective-C - 首页
- Objective-C - 概述
- Objective-C - 环境搭建
- Objective-C - 程序结构
- Objective-C - 基本语法
- Objective-C - 数据类型
- Objective-C - 变量
- Objective-C - 常量
- Objective-C - 运算符
- Objective-C - 循环
- Objective-C - 条件判断
- Objective-C - 函数
- Objective-C - 块 (Blocks)
- Objective-C - 数字
- Objective-C - 数组
- Objective-C - 指针
- Objective-C - 字符串
- Objective-C - 结构体
- Objective-C - 预处理器
- Objective-C - Typedef
- Objective-C - 类型转换
- Objective-C - 日志处理
- Objective-C - 错误处理
- 命令行参数
- 高级Objective-C
- Objective-C - 类与对象
- Objective-C - 继承
- Objective-C - 多态
- Objective-C - 数据封装
- Objective-C - 分类 (Categories)
- Objective-C - 伪装 (Posing)
- Objective-C - 扩展 (Extensions)
- Objective-C - 协议 (Protocols)
- Objective-C - 动态绑定
- Objective-C - 组合对象
- Obj-C - Foundation框架
- Objective-C - 快速枚举
- Obj-C - 内存管理
- Objective-C有用资源
- Objective-C - 快速指南
- Objective-C - 有用资源
- Objective-C - 讨论
Objective-C继承
面向对象编程中最重要的概念之一就是继承。继承允许我们根据另一个类来定义一个类,这使得创建和维护应用程序更加容易。这也提供了代码功能重用和快速实现时间的机会。
创建类时,程序员可以指定新类应该继承现有类的成员,而不是完全编写新的数据成员和成员函数。这个现有类称为基类,新类称为派生类。
继承的概念实现了is a关系。例如,哺乳动物是动物,狗是哺乳动物,因此狗也是动物,依此类推。
基类和派生类
Objective-C只允许多级继承,即它只能有一个基类,但允许多级继承。Objective-C中的所有类都派生自超类NSObject。
@interface derived-class: base-class
考虑一个基类Person及其派生类Employee,如下所示:
#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;
}
编译并执行上述代码后,将产生以下结果:
2013-09-22 21:20:09.842 Inheritance[349:303] Base class Person Object 2013-09-22 21:20:09.844 Inheritance[349:303] Name: Raj 2013-09-22 21:20:09.844 Inheritance[349:303] Age: 5 2013-09-22 21:20:09.845 Inheritance[349:303] Inherited Class Employee Object 2013-09-22 21:20:09.845 Inheritance[349:303] Name: Raj 2013-09-22 21:20:09.846 Inheritance[349:303] Age: 5 2013-09-22 21:20:09.846 Inheritance[349:303] Education: MBA
访问控制和继承
如果派生类在接口类中定义,则它可以访问其基类的所有私有成员,但它不能访问在实现文件中定义的私有成员。
我们可以按照谁可以访问它们来总结不同的访问类型:
派生类继承所有基类方法和变量,但以下情况例外:
使用扩展在实现文件中声明的变量不可访问。
使用扩展在实现文件中声明的方法不可访问。
如果继承的类实现了基类中的方法,则执行派生类中的方法。
广告