年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2330|回复: 2

第十五讲:Objective-C特性之继承与重写

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-1-11 15:55:31 | 显示全部楼层 |阅读模式
    1、创建两个类,Father和Son
    2、Father具有两个方法:run和jump;
    3、Son继承Father类;
    4、Son重写方法jump;
    代码如下:
    Father.h
    1. //
    2. //  Father.h
    3. //  Rewrite
    4. //
    5. //  Created by yusian on 14-1-11.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface Father : NSObject
    10. - (void) run;
    11. - (void) jump;
    12. @end
    复制代码
    Father.m
    1. //
    2. //  Father.m
    3. //  Rewrite
    4. //
    5. //  Created by yusian on 14-1-11.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "Father.h"
    9. @implementation Father
    10. - (void) run{
    11.     NSLog(@"He is running!");
    12. }
    13. - (void) jump{
    14.     NSLog(@"I can jump 1.2m");
    15. }
    16. @end
    复制代码
    Son.h
    1. //
    2. //  Son.h
    3. //  Rewrite
    4. //
    5. //  Created by yusian on 14-1-11.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "Father.h"
    9. @interface Son : Father
    10. @end
    复制代码
    Son.m
    1. //
    2. //  Son.m
    3. //  Rewrite
    4. //
    5. //  Created by yusian on 14-1-11.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "Son.h"
    9. @implementation Son
    10. - (void) jump{
    11.     NSLog(@"I can jump 1.8m");
    12. }
    13. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Rewrite
    4. //
    5. //  Created by yusian on 14-1-11.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "Father.h"
    10. #import "Son.h"
    11. int main(int argc, const char * argv[])
    12. {
    13.     @autoreleasepool {
    14.         
    15.         Father * father = [[Father alloc] init];
    16.         Son * son = [[Son alloc] init];
    17.         [father run];
    18.         [father jump];
    19.         [son run];
    20.         [son jump];
    21.         
    22.         [father release];
    23.         [son release];
    24.         
    25.     }
    26.     return 0;
    27. }
    复制代码
    输出结果:
    1. 2014-01-11 15:48:32.380 Rewrite[4155:303] He is running!
    2. 2014-01-11 15:48:32.382 Rewrite[4155:303] I can jump 1.2m
    3. 2014-01-11 15:48:32.383 Rewrite[4155:303] He is running!
    4. 2014-01-11 15:48:32.383 Rewrite[4155:303] I can jump 1.8m
    5. Program ended with exit code: 0
    复制代码



  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-1-11 15:56:45 | 显示全部楼层

    几点说明

    本帖最后由 Sian 于 2014-1-11 16:00 编辑

    1、子类能够继承父类中的所有方法,方法默认为public属性;
    2、变量默认为protected属性,子类可直接继承,但如果父类中申请变量为prviate,则不能被继承;
    3、子类如果需要访问父类中的private变量,可通过父类的方法来实现;
    4、Son子类中不需要申明父类中的方法,可直接重写并覆盖。
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-1-11 16:06:42 | 显示全部楼层

    Objective-C基本特性之虚方法

    OC中的方法都为虚方法,什么叫虚方法,以上述为例,用一个简单的操作来进行说明;在main函数中插入两行代码:
    1. Father * p = son;
    2. [p jump];
    复制代码
    输出的结果为:
    1. 2014-01-11 16:02:25.227 Rewrite[4217:303] I can jump 1.8m
    复制代码
    即,父类创建的对象可以直接访问子类的方法;
    调用方法时不看指针,看对象,这类方法叫虚方法。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-2 10:25 , Processed in 0.045458 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表