Object-C——三大特性之继承

二、继承

   继承是指一个新类中和上一个类中的部分属性相同,为了简化代码,我们把相同的属性从先前的那个类中传递过来。

      1、继承的好处:

       ①  抽取重复代码

       ②  建立了类之间的关系

       ③  子类可以拥有父类中的所有成员变量和方法

      2、注意点

        基本上所有类的根类是NSObject

      3、继承的执行过程

       ①  子类方法和属性的访问过程:如果子类没有,就去访问父类的

       ② 父类被继承了还是能照常使用的

       ③ 父类的静态方法

       ④  画继承结构图,从子类抽取到父类

       ⑤  NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new

 1 /********Animal的声明*******/
 2 @interface Animal : NSObject
 3 {
 4     int _age;
 5     double _weight;
 6 }
 7 
 8 - (void)setAge:(int)age;
 9 - (int)age;
10 
11 - (void)setWeight:(double)weight;
12 - (double)weight;
13 @end
14 
15 /********Animal的实现*******/
16 @implementation Animal
17 - (void)setAge:(int)age
18 {
19     _age = age;
20 }
21 - (int)age
22 {
23     return _age;
24 }
25 
26 - (void)setWeight:(double)weight
27 {
28     _weight = weight;
29 }
30 - (double)weight
31 {
32     return _weight;
33 }
34 @end
35 
36 /********Dog*******/
37 // : Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法
38 // Animal称为Dog的父类
39 // Dog称为Animal的子类
40 @interface Dog : Animal
41 @end
42 
43 @implementation Dog
44 @end

     4、继承的使用场合

      ①  当两个类拥有相同属性和方法的时候,就可以将相同的东西抽取到一个父类中

      ②  当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类

 1 A
 2  {
 3     int _age;
 4     int _no;
 5  }
 6  
 7  B : A
 8  {
 9     int _weight;
10  }
11  

     5、重写
          子类重新实现父类中的某个方法,覆盖父类以前的做法
      1、注意
        ① 父类必须声明在子类的前面
        ② 子类不能拥有和父类相同的成员变量
        ③ 调用某个方法时,优先去当前类中找,如果找不到,去父类中找
      2、坏处:耦合性太强

 1 // 不允许子类和父类拥有相同名称的成员变量
 2 // Student
 3 @interface Student : Person
 4 {
 5     int _no;
 6     // int _age;
 7 }
 8 
 9 + (void)test2;
10 
11 @end
12 
13 @implementation Student
14 // 重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
15 - (void)run
16 {
17     NSLog(@"student---跑");
18 }
19 
20 + (void)test2
21 {
22     [self test];
23 }
24 @end

  6、继承和组合

      继承:xx 是 xxx
      组合:xxx 拥有 xxx

 1 //组合举例
 2 @interface Score : NSObject
 3 {
 4     int _cScore;
 5     int _ocScore;
 6 }
 7 @end
 8 
 9 @implementation Score
10 @end
11 
12 @interface Student : NSObject
13 {
14     // 组合
15     Score *_score;
16 
17     int _age;
18 }
19 @end
20 
21 @implementation Student
22 
23 @end

   7、 super关键字
         super的作用
         1、 直接调用父类中的某个方法
         2、super处在对象方法中,那么就会调用父类的对象方法,super处在类方法中,那么就会调用父类的类方法
        3、使用场合:子类重写父类的方法时想保留父类的一些行为

 1 #import <Foundation/Foundation.h>
 2 @interface Zoombie : NSObject
 3 - (void)walk;
 4 
 5 + (void)test;
 6 - (void)test;
 7 
 8 @end
 9 
10 @implementation Zoombie
11 - (void)walk
12 {
13     NSLog(@"往前挪两步******");
14 }
15 
16 + (void)test
17 {
18     NSLog(@"Zoombie+test");
19 }
20 
21 - (void)test
22 {
23     NSLog(@"Zoombie-test");
24 }
25 @end
26 
27 // 跳跃僵尸
28 @interface JumpZoombie : Zoombie
29 + (void)haha;
30 - (void)haha2;
31 @end
32 
33 
34 @implementation JumpZoombie
35 
36 + (void)haha
37 {
38     [super test];
39 }
40 
41 - (void)haha2
42 {
43     [super test];
44 }
45 
46 - (void)walk
47 {
48     // 跳两下
49     NSLog(@"跳两下");
50     
51     // 走两下(直接调用父类的walk方法)
52     [super walk];
53     //NSLog(@"往前挪两步----");
54 
55 }
56 @end
原文地址:https://www.cnblogs.com/gaizuojia/p/4357208.html