zoukankan      html  css  js  c++  java
  • 【学习笔记】【OC语言】继承

    1.继承的基本用法

     1 #import <Foundation/Foundation.h>
     2 /*
     3  1.继承的好处:
     4  1> 抽取重复代码
     5  2> 建立了类之间的关系
     6  3> 子类可以拥有父类中的所有成员变量和方法
     7  
     8  2.注意点
     9  1> 基本上所有类的根类是NSObject
    10  */
    11 
    12 
    13 /********Animal的声明*******/
    14 @interface Animal : NSObject
    15 {
    16     int _age;
    17     double _weight;
    18 }
    19 
    20 - (void)setAge:(int)age;
    21 - (int)age;
    22 
    23 - (void)setWeight:(double)weight;
    24 - (double)weight;
    25 @end
    26 
    27 /********Animal的实现*******/
    28 @implementation Animal
    29 - (void)setAge:(int)age
    30 {
    31     _age = age;
    32 }
    33 - (int)age
    34 {
    35     return _age;
    36 }
    37 
    38 - (void)setWeight:(double)weight
    39 {
    40     _weight = weight;
    41 }
    42 - (double)weight
    43 {
    44     return _weight;
    45 }
    46 @end
    47 
    48 /********Dog*******/
    49 // : Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法
    50 // Animal称为Dog的父类
    51 // Dog称为Animal的子类
    52 @interface Dog : Animal
    53 @end
    54 
    55 @implementation Dog
    56 @end
    57 
    58 /********Cat*******/
    59 @interface Cat : Animal
    60 @end
    61 
    62 @implementation Cat
    63 @end
    64 
    65 int main()
    66 {
    67     Dog *d = [Dog new];
    68     
    69     [d setAge:10];
    70     
    71     NSLog(@"age=%d", [d age]);
    72     return 0;
    73 }

    子类方法和属性的访问过程:如果子类没有,就去访问父类的
    父类被继承了还是能照常使用的
    父类的静态方法
    画继承结构图,从子类抽取到父类
    NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new

    2.继承的专业术语
    父类超类  superclass
    子类  subclasssubclasses

    3.继承的细节
    单继承
    子类和父类不能有相同的成员变量
    方法的重写

    4.super关键字
    分别调用父类的对象方法和类方法

     1 /*
     2  僵尸
     3  
     4  跳跃僵尸、舞王僵尸、铁桶僵尸
     5  */
     6 #import <Foundation/Foundation.h>
     7 
     8 /*
     9  super的作用
    10  1.直接调用父类中的某个方法
    11  2.super处在对象方法中,那么就会调用父类的对象方法
    12    super处在类方法中,那么就会调用父类的类方法
    13  
    14  3.使用场合:子类重写父类的方法时想保留父类的一些行为
    15  */
    16 
    17 // 僵尸
    18 @interface Zoombie : NSObject
    19 - (void)walk;
    20 
    21 + (void)test;
    22 - (void)test;
    23 
    24 @end
    25 
    26 @implementation Zoombie
    27 - (void)walk
    28 {
    29     NSLog(@"往前挪两步******");
    30 }
    31 
    32 + (void)test
    33 {
    34     NSLog(@"Zoombie+test");
    35 }
    36 
    37 - (void)test
    38 {
    39     NSLog(@"Zoombie-test");
    40 }
    41 @end
    42 
    43 // 跳跃僵尸
    44 @interface JumpZoombie : Zoombie
    45 + (void)haha;
    46 - (void)haha2;
    47 @end
    48 
    49 
    50 @implementation JumpZoombie
    51 
    52 + (void)haha
    53 {
    54     [super test];
    55 }
    56 
    57 - (void)haha2
    58 {
    59     [super test];
    60 }
    61 
    62 - (void)walk
    63 {
    64     // 跳两下
    65     NSLog(@"跳两下");
    66     
    67     // 走两下(直接调用父类的walk方法)
    68     [super walk];
    69     //NSLog(@"往前挪两步----");
    70 
    71 }
    72 @end
    73 
    74 int main()
    75 {
    76     //[JumpZoombie haha];
    77     JumpZoombie *jz = [JumpZoombie new];
    78     
    79     [jz haha2];
    80     
    81     return 0;
    82 }

    5.继承的好处
    不改变原来模型的基础上,拓充方法
    建立了类与类之间的联系
    抽取了公共代码
    坏处:耦合性强

    6.继承的使用场合
    它的所有属性都是你想要的,一般就继承
    它的部分属性是你想要的,可以抽取出另一个父类

    7.代码

     1 /*
     2  1.重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
     3  2.注意
     4  1> 父类必须声明在子类的前面
     5  2> 子类不能拥有和父类相同的成员变量
     6  3> 调用某个方法时,优先去当前类中找,如果找不到,去父类中找
     7  
     8  2.坏处:耦合性太强
     9  */
    10 
    11 #import <Foundation/Foundation.h>
    12 // Person
    13 @interface Person : NSObject
    14 {
    15     int _age;
    16 }
    17 
    18 - (void)setAge:(int)age;
    19 - (int)age;
    20 
    21 - (void)run;
    22 
    23 + (void)test;
    24 
    25 @end
    26 
    27 @implementation Person
    28 
    29 + (void)test
    30 {
    31     NSLog(@"Person+test");
    32 }
    33 
    34 - (void)run
    35 {
    36     NSLog(@"person---跑");
    37 }
    38 
    39 - (void)setAge:(int)age
    40 {
    41     _age = age;
    42 }
    43 - (int)age
    44 {
    45     return _age;
    46 }
    47 @end
    48 
    49 // 不允许子类和父类拥有相同名称的成员变量
    50 // Student
    51 @interface Student : Person
    52 {
    53     int _no;
    54     // int _age;
    55 }
    56 
    57 + (void)test2;
    58 
    59 @end
    60 
    61 @implementation Student
    62 // 重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
    63 - (void)run
    64 {
    65     NSLog(@"student---跑");
    66 }
    67 
    68 + (void)test2
    69 {
    70     [self test];
    71 }
    72 @end
    73 
    74 
    75 int main()
    76 {
    77     [Student test2];
    78     
    79 //    Student *s = [Student new];
    80 //    
    81 //    [s run];
    82     
    83     return 0;
    84 }

    继承的使用场合

     1 /*
     2 1.继承的使用场合
     3  1> 当两个类拥有相同属性和方法的时候,就可以将相同的东西抽取到一个父类中
     4  2> 当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类
     5  A
     6  {
     7     int _age;
     8     int _no;
     9  }
    10  
    11  B : A
    12  {
    13     int _weight;
    14  }
    15  
    16  // 继承:xx 是 xxx
    17  // 组合:xxx 拥有 xxx
    18  
    19  2.组合
    20  A
    21  {
    22      int _age;
    23      int _no;
    24  }
    25  
    26  B
    27  {
    28      A *_a;
    29      int _weight;
    30  }
    31 */
    32 
    33 @interface Score : NSObject
    34 {
    35     int _cScore;
    36     int _ocScore;
    37 }
    38 @end
    39 
    40 @implementation Score
    41 @end
    42 
    43 @interface Student : NSObject
    44 {
    45     // 组合
    46     Score *_score;
    47 //    int _cScore;
    48 //    int _ocScore;
    49     int _age;
    50 }
    51 @end
    52 
    53 @implementation Student
    54 
    55 @end
     
  • 相关阅读:
    Struts2+Spring+Ibatis集成合并
    spring多个定时任务quartz配置
    Quartz作业调度框架
    百度搜索URL参数含义
    代理IP抓取
    解决HttpWebRequest和HtmlAgilityPack采集网页中文乱码问题
    移动端上传头像-相册、拍摄-旋转
    订单倒计时
    css flex布局 实例
    currentTarget与target
  • 原文地址:https://www.cnblogs.com/dssf/p/4649742.html
Copyright © 2011-2022 走看看