【object-c 学习笔记】第4章 继承

继承 inheritance
1、概念:

2、声明新类的方法:@interface Circle:NSObjcet
冒号后边的标识符是需要继承的类

3、有关术语
超类(superclass):是继承的类;
父类(parent class):是超类的另一种表达方式 ;
子类(subclass):是执行继承的类 ;
孩子类(child class):子类的另一种表达方式;

4、objcet-c 继承的实例

【案例一】

 1 //程序中定义了两个类,分别是ClassA和ClassB;ClassA的父类是NSObject ClassB继承了ClassA,间接继承了NSObject;
 2 //interface 用来定义;implementation用来实现类的方法
 3 @interface ClassA : NSObject
 4 {
 5     int n;
 6 }
 7 -(int)initVar;
 8 @end
 9 
10 @implementation ClassA
11 
12 -(void)initVar
13 {
14     n=406;
15 }
16 @end
17 
18 @interface classB : ClassA
19 -(void)printVar;
20 @end
21 
22 @implementation classB
23 
24 -(void)printVar
25 {
26     NSLog(@"n=%i",n);
27 }
28 @end
29 
30 int main(int argc, char * argv[]) {
31     @autoreleasepool {
32         classB *clsB = [classB new];
33         [clsB initVar];
34         [clsB printVar];
35     }

运行后的结果:

2015-04-21 22:29:01.619 DoudouInheritanceProject[6027:704463] n=406

下面来解释下这个程序,该程序中定义了两个类ClassA和ClassB,ClassA的父类是NSObject,这是object-c的一个根类,ClassB 继承自ClassA,那么也间接继承了NSObject类;定义了classA的接口时,我们定义的一个变量n,那么子类ClassB才能访问它,classA中定义了方法initVar,而在ClassB中定义了方法printVar。在执行主函数时,首先创建了classB对象,使用initVar来初始化参数,initVar是ClassA中的方法,此时classB类在引用指针来调用就体现了继承的观点,然后再执行printVar方法,打印出结果。

 【案例二】

先定义一个总的Shape父类,定义好方法和属性,然后继承父类。代码如下:

 1 #import <Foundation/Foundation.h>
 2 /* enum 枚举类型 */
 3 //定义绘制图形的类型: 圆形,矩形,椭圆形
 4 typedef enum{
 5     kCircle,
 6     kRectangle,
 7     kEgg
 8 } ShapeType;
 9 
10 //定义绘制图形的颜色: 红色,绿色和蓝色
11 typedef enum{
12     kRedColor,
13     kGreenColor,
14     kBlueColor
15 } ShapeColor;
16 
17 /* struct 结构体 */
18 //定义图形的基本属性
19 typedef struct{
20     int x, y, width, height;
21 } ShapeRect;
22 
23 NSString *colorName (ShapeColor fillColor)
24 {
25     switch(fillColor)
26     {
27         case kRedColor:
28             return @"red";
29             break;
30         case kGreenColor:
31             return @"green";
32             break;
33         case kBlueColor:
34             return @"blue";
35             break;
36     }
37 }
38 
39 /* 定义Shape父类*/
40 @interface Shape: NSObject{
41     ShapeColor fillColor;
42     ShapeRect bounds;
43 }
44 -(void) setFillColor:(ShapeColor) fillColor;
45 -(void) setBounds:(ShapeRect) bounds;
46 -(void) draw;
47 @end //Shape
48 
49 /* 实现Shape父类 */
50 @implementation Shape
51 -(void) setFillColor:(ShapeColor) c
52 {
53     fillColor = c;
54 }
55 -(void) setBounds:(ShapeRect) b
56 {
57     bounds = b;
58 }
59 -(void) draw{
60 }
61 @end

 然后,我们分别定义Circle,Rectangle和Egg子类,代码如下:

 1 /*定义Circle,继承Shape父类*/
 2 @interface Circle: Shape
 3 @end
 4 
 5 /*定义Rectangle,继承Shape父类*/
 6 @interface Rectangle: Shape
 7 @end
 8 
 9 /*定义Egg,继承Shape父类*/
10 @interface Egg: Shape
11 @end

接下来,对子类的draw方法进行实现,代码如下:

 1 @implementation Circle
 2 -(void) draw
 3 {
 4     NSLog(@"drawing a circle at (%d %d %d %d) in %@",
 5           bounds.x,
 6           bounds.y,
 7           bounds.height,
 8           bounds.width,
 9           colorName(fillColor));
10 }
11 @end
12 
13 @implementation Rectangle
14 -(void) draw
15 {
16     NSLog(@"drawing a rectangle at (%d %d %d %d) in %@",
17           bounds.x,
18           bounds.y,
19           bounds.height,
20           bounds.width,
21           colorName(fillColor));
22 }
23 @end
24 
25 @implementation Egg
26 -(void) draw
27 {
28     NSLog(@"drawing an egg at (%d %d %d %d) in %@",
29           bounds.x,
30           bounds.y,
31           bounds.height,
32           bounds.width,
33           colorName(fillColor));
34 }
35 @end

 运行结果如下:

在上面的例子中,我们在子类中重新实现了draw的方法,这个过程叫重写方法

执行的时候,如果子类中有重新定义父类中的方法,那么就会先去执行子类方法,父类中的同名方法则会别忽略。如果子类方法找不到,再去执行父类中定义的方法。

 
 
原文地址:https://www.cnblogs.com/sallyWei/p/4446016.html