Objective-C中的继承

简介: #import // -------------------------------------------------- // constants for the different kinds of shapes and their colors typedef enum {...
#import <Foundation/Foundation.h>

// --------------------------------------------------
// constants for the different kinds of shapes and their colors

typedef enum {
    kRedColor,
    kGreenColor,
    kBlueColor
} ShapeColor;


// --------------------------------------------------
// Shape bounding rectangle


typedef struct {
    int x, y, width, height;
} ShapeRect;


// --------------------------------------------------
// convert from the ShapeColor enum value to a human-readable name

NSString *colorName (ShapeColor color)
{
    NSString *colorName;
    
    switch (color) {
        case kRedColor:
            colorName = @"red";
            break;
        case kGreenColor:
            colorName = @"green";
            break;
        case kBlueColor:
            colorName = @"blue";
            break;
        default:
            colorName = @"oops! Unexpected color";
            break;
    }
    
    return (colorName);
    
} // colorName


// --------------------------------------------------
// Shapes base class

@interface Shape : NSObject
{
    ShapeColor  fillColor;
    ShapeRect   bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;

@end // Shape


@implementation Shape

- (void) setFillColor: (ShapeColor) c
{
    fillColor = c;
} // setFillColor

- (void) setBounds: (ShapeRect) b
{
    bounds = b;
} // setBounds

- (void) draw
{
} // draw

@end // Shape




// --------------------------------------------------
// All about Triangles

@interface Triangle : Shape
@end // Triangle


@implementation Triangle

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

@end // Triangle




// --------------------------------------------------
// All about Circles

@interface Circle : Shape
@end // Circle


@implementation Circle

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

@end // Circle




// --------------------------------------------------
// All about Rectangles

@interface Rectangle : Shape
@end // Rectangle


@implementation Rectangle

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

@end // Rectangle


// --------------------------------------------------
// All about OblateSphereoids

@interface OblateSphereoid : Shape
@end // OblateSphereoid


@implementation OblateSphereoid

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

@end // OblateSphereoid



// --------------------------------------------------
// Draw the shapes

void drawShapes (id shapes[], int count)
{
    for (int i = 0; i < count; i++)
    {
        id shape = shapes[i];
        [shape draw];
    }
    
} // drawShapes



// --------------------------------------------------
// The main function.  Make the shapes and draw them

int main (int argc, const char * argv[]) 
{
    id shapes[4]; // I'm different!
    
    ShapeRect rect0 = { 0, 0, 10, 30 };
    shapes[0] = [Circle new];
    [shapes[0] setBounds: rect0];
    [shapes[0] setFillColor: kRedColor];
    
    ShapeRect rect1 = { 30, 40, 50, 60 };
    shapes[1] = [Rectangle new];
    [shapes[1] setBounds: rect1];
    [shapes[1] setFillColor: kGreenColor];
    
    ShapeRect rect2 = { 15, 19, 37, 29 };
    shapes[2] = [OblateSphereoid new];
    [shapes[2] setBounds: rect2];
    [shapes[2] setFillColor: kBlueColor];
    
    ShapeRect rect3 = { 47, 32, 80, 50 };    // I'm new!
    shapes[3] = [Triangle new];
    [shapes[3] setBounds: rect3];
    [shapes[3] setFillColor: kRedColor];
    
    drawShapes (shapes, 4); // I'm different!
    
    return (0);
    
} // main

 

目录
相关文章
|
Java 调度 iOS开发
|
Java iOS开发
Objective-C的继承与组合
Objective-C的继承与组合 Objective-C与Java继承上的区别 区别 Objective-C Java 成员变量 Objective-C继承不允许子...
1039 0
|
iOS开发 调度 编译器
【《Objective-C基础教程 》笔记ch04】(五)OC中的继承inheritance机制
一、为什么需要继承                            使用继承一方面继承了父类的特性,另一方便解决了重复代码维护问题。 二、继承之语法          1、 @interface 子类名:父类名          2、 OC只支持单继承,不支持继承多个父类。
1043 0
|
iOS开发
objective-C 的OOP(上)--类定义、继承及方法调用
上一篇展示了如何用传统的“面向过程编程方法”,实现画“矩形”、“圆”、“椭圆”,这一篇看下如何改用OOP的方法来实现: 因为要用到“颜色”以及“矩形区域”二个枚举,先把他们抽出来单独放在CommDef.
959 0
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
1339 2
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
iOS Objective-C 应用连接Azure Storage时,若不关闭账号的匿名访问,程序能正常运行。但关闭匿名访问后,上传到容器时会出现错误:“Public access is not permitted”。解决方法是将创建容器时的公共访问类型从`AZSContainerPublicAccessTypeContainer`改为`AZSContainerPublicAccessTypeOff`,以确保通过授权请求访问。
270 0
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错