当前位置: 首页 > news >正文

Ojective-C学习笔记(4)关于面向对象编程

在面向对象编程中使用间接

在学习之前,先看一个过程式编程的例子。

//
//  main.m
//  OOP
//
//  Created by ccyag on 21/4/18.
//  Copyright © 2018年 ccyag. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum{
    kCircle,
    kRectangle,
    kTriangle
} ShapeType;

typedef enum{
    kRed,
    kGreen,
    kBlue
} ShapeColor;

NSString *colorName(ShapeColor color){
    switch (color) {
        case kRed:
            return @"red";
            break;
        case kGreen:
            return @"green";
            break;
            case kBlue:
        return @"blue";
            break;
    }
}

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

typedef struct {
    ShapeType type;
    ShapeColor color;
    ShapeRect bounds;
} Shape;

void drawCircle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a circle at(%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawRectangle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a rectangle at(%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawTriangle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a triangle at(%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawShapes(Shape shapes[], int count){
    for(int i = 0; i < count; i++){
        switch (shapes[i].type) {
            case kCircle:
                drawCircle(shapes[i].bounds, shapes[i].color);
                break;
            case kRectangle:
                drawRectangle(shapes[i].bounds, shapes[i].color);
                break;
            case kTriangle:
                drawTriangle(shapes[i].bounds, shapes[i].color);
                break;
        }
    }
}

int main(int argc, const char * argv[]) {
    Shape shapes[3];
    ShapeRect rect0 = {0, 0, 30, 30};
    shapes[0].type = kCircle;
    shapes[0].color = kRed;
    shapes[0].bounds = rect0;
    
    ShapeRect rect1 = {50, 50, 60, 80};
    shapes[1].type = kRectangle;
    shapes[1].color = kGreen;
    shapes[1].bounds = rect1;
    
    ShapeRect rect2 = {100, 100, 120, 150};
    shapes[2].type = kTriangle;
    shapes[2].color = kBlue;
    shapes[2].bounds = rect2;
    
    drawShapes(shapes, 3);
    return 0;
}

运行结果如下:

2018-04-21 14:27:33.330017+0800 OOP[4205:624974] drawing a circle at(0 0 30 30) in red
2018-04-21 14:27:33.330242+0800 OOP[4205:624974] drawing a rectangle at(50 50 60 80) in green
2018-04-21 14:27:33.330255+0800 OOP[4205:624974] drawing a triangle at(100 100 120 150) in blue

这是一段面向过程的代码,功能很简单,实现了图形的绘制功能,每一种图形都有自己的颜色,形状和坐标位置,用到了C语言中的枚举和结构体。
这段代码中有很多重复的地方,而且扩展和维护起来很困难。比如添加一个椭圆图形,完整代码如下:

#import <Foundation/Foundation.h>

typedef enum{
    kCircle,
    kRectangle,
    kTriangle,
    kEllipse
} ShapeType;

typedef enum{
    kRed,
    kGreen,
    kBlue
} ShapeColor;

NSString *colorName(ShapeColor color){
    switch (color) {
        case kRed:
            return @"red";
            break;
        case kGreen:
            return @"green";
            break;
            case kBlue:
        return @"blue";
            break;
    }
}

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

typedef struct {
    ShapeType type;
    ShapeColor color;
    ShapeRect bounds;
} Shape;

void drawCircle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a circle at (%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawRectangle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a rectangle at (%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawTriangle(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a triangle at (%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawEllipse(ShapeRect bounds, ShapeColor color){
    NSLog(@"drawing a Ellipse at (%d %d %d %d) in %@",
          bounds.x, bounds.y, bounds.widht, bounds.height, colorName(color));
}

void drawShapes(Shape shapes[], int count){
    for(int i = 0; i < count; i++){
        switch (shapes[i].type) {
            case kCircle:
                drawCircle(shapes[i].bounds, shapes[i].color);
                break;
            case kRectangle:
                drawRectangle(shapes[i].bounds, shapes[i].color);
                break;
            case kTriangle:
                drawTriangle(shapes[i].bounds, shapes[i].color);
                break;
            case kEllipse:
                drawEllipse(shapes[i].bounds, shapes[i].color);
        }
    }
}

int main(int argc, const char * argv[]) {
    Shape shapes[4];
    ShapeRect rect0 = {0, 0, 30, 30};
    shapes[0].type = kCircle;
    shapes[0].color = kRed;
    shapes[0].bounds = rect0;
    
    ShapeRect rect1 = {50, 50, 60, 80};
    shapes[1].type = kRectangle;
    shapes[1].color = kGreen;
    shapes[1].bounds = rect1;
    
    ShapeRect rect2 = {100, 100, 120, 150};
    shapes[2].type = kTriangle;
    shapes[2].color = kBlue;
    shapes[2].bounds = rect2;
    
    ShapeRect rect3 = {200, 200, 40, 80};
    shapes[3].type = kEllipse;
    shapes[3].color = kRed;
    shapes[3].bounds = rect3;
    
    drawShapes(shapes, 4);
    return 0;
}

需要修改很多地方,修改的地方越多,程序越容易出错。面向对象编程很好地解决了这些问题。在下一篇随笔中将实现面向对象编程。

转载于:https://www.cnblogs.com/ccyag/p/8901226.html

相关文章:

  • I函数
  • 猫狗大战
  • 洛谷 2055 BZOJ 1433 [ZJOI2009]假期的宿舍
  • UVA 10891 Game of Sum(区间DP(记忆化搜索))
  • Python学习4,字符串
  • BZOJ 3097: Hash Killer I
  • [转组第一天] | 调研XSS攻击
  • 2018年最新搜索引擎转跳JavaScript代码(竞价广告专用)
  • Java多线程实现的三种方式
  • 服务端渲染
  • 【转】数据库范式(1NF 2NF 3NF BCNF)
  • 父元素与子元素之间的margin-top问题(css hack)
  • 20180427积累
  • 关于sqoop --split-by 及 -m的理解
  • 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告
  • 2017年终总结、随想
  • C++11: atomic 头文件
  • CentOS从零开始部署Nodejs项目
  • HTML5新特性总结
  • JavaScript HTML DOM
  • js学习笔记
  • Magento 1.x 中文订单打印乱码
  • overflow: hidden IE7无效
  • Puppeteer:浏览器控制器
  • TypeScript迭代器
  • vue 配置sass、scss全局变量
  • WePY 在小程序性能调优上做出的探究
  • 汉诺塔算法
  • 一个SAP顾问在美国的这些年
  • 用Canvas画一棵二叉树
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • 组复制官方翻译九、Group Replication Technical Details
  • #define与typedef区别
  • #控制台大学课堂点名问题_课堂随机点名
  • (3)选择元素——(14)接触DOM元素(Accessing DOM elements)
  • (4)logging(日志模块)
  • (C++)八皇后问题
  • (四)Linux Shell编程——输入输出重定向
  • (转) RFS+AutoItLibrary测试web对话框
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • ;号自动换行
  • @Repository 注解
  • [ 第一章] JavaScript 简史
  • []C/C++读取串口接收到的数据程序
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [acm算法学习] 后缀数组SA
  • [AIGC] Java 和 Kotlin 的区别
  • [c]扫雷
  • [CentOs7]iptables防火墙安装与设置
  • [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
  • [c语言]小课堂 day2
  • [Electron]ipcMain.on和ipcMain.handle的区别