自学iOS开发系列----OC(继承)

来源:互联网 发布:淘宝数据包采集 编辑:程序博客网 时间:2024/06/08 02:10

继承
继承也可以被称作派生,父类拥有的属性和方法,子类直接获得,这个过程叫做继承。
子类在父类基础上,衍生出了自己的属性和方法,称为派生。
继承和派生是描述一件事物的两种侧重

1.从生活角度理解继承
生物 -> 动物 -> 人 -> 男人
宇宙 -> 银河系 -> 太阳系
生物 -> 植物 -> 花 -> 牡丹、玫瑰

2.父类和子类的称呼
父类:父类 超类 基类
子类:子类 子类 派生类

3.子类重写父类方法
有时候从父类继承到的方法,并不适合子类,子类可以重写这个方法。重写时,在子类中不用重复声明。很多时候,子类的方法只需要在父类的同名方法中增补内容,可以通过关键字【super】调用父类的方法,再增添自己都有的内容。

4. 继承的作用
①创建大量的相似类(较少使用)
【例】创建象棋中的棋子,车马炮 可以先创建棋子类,作为父类
②继承一个官方或第三方类,添加属性方法,创建一个更符合当前程序的新类(最常用)
【例】继承UIView 父类根据相应业务逻辑和界面要求创建子控件
【注】NSString NSArray NSDictionary 都是【工厂类】不能被继承,即使被继承,不能使用父类方法

5.OC不能sizeof一个对象大小或一个类的大小,因为OC的动态继承编译机制,所谓动态继承编译机制,就是说在编译时,不能确立类之间的继承关系,自然无法确定类的大小,只有在运行时,才能确立类之间的继承关系,不能在编译时确定大小,自然不能创建在栈中。

6.理解父类和子类
新建父类:Person类
Person.h文件

#import <Foundation/Foundation.h>@interface Person : NSObject {    /**     * @public作用范围最大,在任何地方     * @protected作用范围在自身类和继承自己的子类,什么都不写,默认是此属性     * @private作用范围只能在自身类     */    @public    NSString * _name;    @protected    int _age;    @private    float _height;}- (void)printInfo;@end

Person.m文件

#import "Person.h"@implementation Person- (void)printInfo {    NSLog(@"公开的方法");}- (void)personPrivate {    NSLog(@"私有的方法");}@end

新建子类:Student类
1)command+N
2)Cocoa Touch Class
Cocoa Touch Class
3)输入即将创建的子类名和继承的父类名
子类名和父类名
Student.h文件

#import "Person.h"@interface Student : Person- (void)studentMethod;@end

Student.m文件

#import "Student.h"@implementation Student- (void)studentMethod {    NSLog(@"我是自己的方法");}- (void)printInfo {    NSLog(@"我是继承父类之后的方法");}@end

main.m文件

#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {       Student * student = [[Student alloc] init];       [student printInfo];       [student studentMethod];    }    return 0;}

7.继承和多态
重写是多态的一种,多态就是同一个声明不同的实现

新建父类Worker类
Worker.h类

#import <Foundation/Foundation.h>@interface Worker : NSObject {    @public    int _money;    @protected    NSString * _name;    @private    NSString * _job;}- (void)show;- (void)printInfo;@end

Worker.m类

#import "Worker.h"@implementation Worker- (void)printInfo {    _job = @"工作";}- (void)show {    NSLog(@"你好 世界!");}@end

新建子类Teacher类
Teacher.h文件

#import "Worker.h"@interface Teacher : Worker@end

Teacher.m文件

#import "Teacher.h"@implementation Teacher- (void)show {    NSLog(@"早安 世界");}- (void)printInfo {    NSLog(@"name = %@ job = %@ money = %d",_name = @"庭勇",_job = @"教师",_money = 1000);}@end

新建子类Singer类
Singer.h文件

#import "Worker.h"@interface Singer : Worker@end

Singer.m文件

#import "Singer.h"@implementation Singer- (void)printInfo {    NSLog(@"name = %@ job = %@",_name = @"王心凌",_job = @"歌手");}@end

main.m文件

#import <Foundation/Foundation.h>#import "Teacher.h"#import "Singer.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Teacher * teacher = [[Teacher alloc] init];                [teacher show];        [teacher printInfo];        Singer * singer = [[Singer alloc] init];        [singer show];        [singer printInfo];    }    return 0;}

【注】子类实现父类已声明的方法时,如果调用[super ]方法则可以在继承父类方法实现的基础上添加自己的实现方法;如果不调用[super ]方法则会覆盖父类的方法。

小练习
更具以下提示编写程序
①声明一个Child类,Child类中有一个成员方法-(void)answer
②声明一个Boy类,父类为Child,需要重写answer方法,回答男孩儿的爱好
③声明一个Girl类,父类为Child,需要重写answer方法,回答女孩儿的爱好
④声明一个Teacher类(父类为NSObject),Teacher中有一个方法-(void)askChild:(Child*)child。方法实现中让child调用answer方法

参考答案(一万个读者有一万个哈姆雷特,一万个程序员有一万种编码风格,答案仅供参考)
①新建Child类
Child.h

#import <Foundation/Foundation.h>@interface Child : NSObject- (void)answer;@end

Child.m

#import "Child.h"@implementation Child- (void)answer {    NSLog(@"儿童喜欢什么?");}@end

②新建Boy类
Boy.h

#import "Child.h"@interface Boy : Child@end

Boy.m

#import "Boy.h"@implementation Boy- (void)answer {    NSLog(@"男孩儿喜欢打篮球");}@end

③新建Girl类
Girl.h

#import "Child.h"@interface Girl : Child@end

Girl.m

#import "Girl.h"@implementation Girl- (void)answer {    NSLog(@"女孩儿喜欢读书");}@end

④新建Teacher类
Teacher.h

#import <Foundation/Foundation.h>#import "Child.h"@interface Teacher : NSObject- (void)askChild:(Child *)child;@end

Teacher.m

#import "Teacher.h"@implementation Teacher- (void)askChild:(Child *)child {    [child answer];}@end

⑤main.m文件

#import <Foundation/Foundation.h>#import "Teacher.h"#import "Boy.h"#import "Girl.h"#import "Child.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Teacher * teacher = [[Teacher alloc] init];        Child * child = [[Child alloc] init];        Boy * boy = [[Boy alloc] init];        [teacher askChild:child];        [boy answer];        Girl * girl = [[Girl alloc] init];        [girl answer];    }    return 0;}
0 0