美文网首页
OC基础语法快速浏览2--继承

OC基础语法快速浏览2--继承

作者: 心明道长 | 来源:发表于2018-06-15 11:47 被阅读0次

继承

在OC中,继承的语法格式。其实,在之前几篇博文中已经看到。声明一个类的时候,@interface Circle :NSObject。这里的冒号,就是继承的标示符,冒号后面的NSObject就是要继承的类,也就是说是父类。注:在OC中,不支持多继承。

#import <Foundation/Foundation.h>
/* 1. enum 枚举类型 */
//定义绘制图形的类型: 圆形,矩形,椭圆形
typedef enum{
    kCircle,
    kRectangle,
    kEgg
} ShapeType;

//定义绘制图形的颜色: 红色,绿色和蓝色
typedef enum{
    kRedColor,
    kGreenColor,
    kBlueColor
} ShapeColor;

/* 2. struct 结构体 */
//定义图形的基本属性
typedef struct{
    int x, y, width, height;
} ShapeRect;

NSString *colorName (ShapeColor fillColor)
{
    switch(fillColor)
    {
        case kRedColor:
            return @"red";
            break;
        case kGreenColor:
            return @"green";
            break;
        case kBlueColor:
            return @"blue";
            break;
    }
}

/* 3. 定义Shape父类*/
@interface Shape: NSObject{
    ShapeColor fillColor;
    ShapeRect bounds;
}
-(void) setFillColor:(ShapeColor) fillColor;
-(void) setBounds:(ShapeRect) bounds;
-(void) draw;
@end //Shape

/* 实现Shape父类 */
@implementation Shape
-(void) setFillColor:(ShapeColor) c
{
    fillColor = c;
}
-(void) setBounds:(ShapeRect) b
{
    bounds = b;
}
-(void) draw{
}
@end

/*定义Circle,继承Shape父类*/
@interface Circle: Shape
@end

/*定义Rectangle,继承Shape父类*/
@interface Rectangle: Shape
@end

/*定义Egg,继承Shape父类*/
@interface Egg: Shape
@end
@implementation Circle
-(void) draw
{
    NSLog(@"drawing a circle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.height,
          bounds.width,
          colorName(fillColor));
}
@end

@implementation Rectangle
-(void) draw
{
    NSLog(@"drawing a rectangle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.height,
          bounds.width,
          colorName(fillColor));
}
@end

@implementation Egg
-(void) draw
{
    NSLog(@"drawing an egg at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.height,
          bounds.width,
          colorName(fillColor));
}
@end

在子类中重新实现了draw的方法,这个过程叫重写方法。执行的时候,如果子类中有重新定义父类中的方法,那么就会先去执行子类方法,父类中的同名方法则会别忽略。如果子类方法找不到,再去执行父类中定义的方法。

在OC中,也提供了既可以重写方法实现,又可以调用父类自身实现的方法。为了调用继承的方法在父类中出现,需要使用super作为方法调用的目标。当我们向super发送信息的时候,实际上是请求OC向该类的父类发送消息。下面就修改一个使用super关键字的例子:

@implementation Circle
-(void) setFillColor:(ShapeColor) c
{
    if(c == kRedColor)
    {
        c = kGreenColor;
    }
    [super setFillColor: c];
}
-(void) draw
{
    NSLog(@"drawing a circle at (%d %d %d %d) in %@",
          bounds.x,
          bounds.y,
          bounds.height,
          bounds.width,
          colorName(fillColor));
}
@end

假如调用的颜色是红色,则返回的是绿色。在函数的结尾处,使用super关键字,通知父类,将新的颜色存储在fillColor变量中。

相关文章

  • OC基础语法快速浏览2--继承

    继承 在OC中,继承的语法格式。其实,在之前几篇博文中已经看到。声明一个类的时候,@interface Circl...

  • OC基础语法快速浏览1

    新建 macOS的命令行工程 8D342DA7-903C-4BEF-B29F-0B33B04B96C8.png 打...

  • 无标题文章

    OC基础语法 //整型 NSIntegera =10; //NSLog是OC里面的打印函数 NSLog(@"a =...

  • oc 基础继承

    定义父类 我们在父类的.h文件中申明了3个属性sex age name 和三个方法 然后我们在 .m文件中实...

  • iOS培训总结

    一:OC基础语法1 //整型 NSInteger a =10; //NSLog是OC里面的打印函数 NSLog(@...

  • 软帝学院:80道java基础部分面试题(四)

    Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集...

  • OC语法基础

    一、属性与成员变量属性表达实例状态,描述类型对外结果,相比直接访问成员变量,属性可以做更多的控制。默认情况下,编译...

  • OC 语法基础

    OC 的语法知识其实有蛮多,以下知识基础的一部分. 目录部分: @ 符号 在 OC 语言中,绝大部分的关键字使用的...

  • OC:基础语法☀️

    版权声明:本文为博主原创文章,未经博主允许不得转载。 学习目标 1.【了解】Objective-C语言简介 2.【...

  • OC基础语法

    // 数组(NSArray/NSMutableArray) // 不可变数组 NSArray *arr...

网友评论

      本文标题:OC基础语法快速浏览2--继承

      本文链接:https://www.haomeiwen.com/subject/jnipeftx.html