面向对象编程(一)
面向过程编程
c语言便是一种面向过程编程的语言。举一段程序代码来更加深刻的认识面向过程。
绘制集合图形:
// // main.m // oc // // Created by Tron on 14-8-8. // Copyright (c) 2014年 Tron. All rights reserved. // #import <Foundation/Foundation.h> typedef enum { circle, rectangle, egg } ShapeType; typedef enum { redColor, greenColor, blueColor } ShapeColor; typedef struct { int x,y,height,width; } ShapeRect; typedef struct { ShapeType type; ShapeColor color; ShapeRect boods; } Shape; NSString *colorName (ShapeColor color) { switch (color) { case redColor: return @"Red"; break; case blueColor: return @"Blue"; break; case greenColor: return @"Green"; break; } } int drawCircle (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a circle at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; } int drawEgg (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a Egg at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; } int drawRect (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a rectangle at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; } void drawShapes (Shape shapes[],int count) { for (int i=0;i<count;i++) { switch (shapes[i].type) { case circle: drawCircle (shapes[i].boods,shapes[i].color); break; case rectangle: drawRect (shapes[i].boods,shapes[i].color); break; case egg: drawEgg (shapes[i].boods,shapes[i].color); break; } } } int main(){ Shape shapes[3]; ShapeRect rect0 = {0,0,10,30}; shapes[0].type = circle; shapes[0].color = redColor; shapes[0].boods = rect0; ShapeRect rect1 = {30,40,50,60}; shapes[1].type = rectangle; shapes[1].color = greenColor; shapes[1].boods = rect1; ShapeRect rect2 = {15,18,37,29}; shapes[2].type = egg; shapes[2].color = blueColor; shapes[2].boods = rect2; drawShapes(shapes, 3); return 0; }
程序的运行结果是这样的:
下面来分析这些函数:
首先用枚举指定了几种可以绘制的形状
typedef enum { circle, rectangle, egg } ShapeType;
接着是枚举几种可填充的颜色
typedef enum { redColor, greenColor, blueColor } ShapeColor;
然后我们来设定要绘制的区域
typedef struct { int x,y,height,width; } ShapeRect;
最后用一个结构体将前面的内容结合起来,整体地描述一个形状
typedef struct { ShapeType type; ShapeColor color; ShapeRect boods; } Shape;
接下来就是在main()函数中声明三种形状以及三种形状地各种属性值。
int main(){ Shape shapes[3]; ShapeRect rect0 = {0,0,10,30}; shapes[0].type = circle; shapes[0].color = redColor; shapes[0].boods = rect0; ShapeRect rect1 = {30,40,50,60}; shapes[1].type = rectangle; shapes[1].color = greenColor; shapes[1].boods = rect1; ShapeRect rect2 = {15,18,37,29}; shapes[2].type = egg; shapes[2].color = blueColor; shapes[2].boods = rect2; drawShapes(shapes, 3); return 0; }
在main()函数中调用了drawShapes()函数,用来绘制图形。
drawShapes()函数线循环检查每个数组中地Shape结构体,再用switch查看type字段并且调用适当的函数绘制图形。
void drawShapes (Shape shapes[],int count) { for (int i=0;i<count;i++) { switch (shapes[i].type) { case circle: drawCircle (shapes[i].boods,shapes[i].color); break; case rectangle: drawRect (shapes[i].boods,shapes[i].color); break; case egg: drawEgg (shapes[i].boods,shapes[i].color); break; } } }
此时,又调用了drawCircle(),drawRect(),drawEgg三个函数,这些函数需要输出形状信息以及传递给它地颜色。
int drawCircle (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a circle at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; } int drawEgg (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a Egg at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; } int drawRect (ShapeRect boods,ShapeColor color) { NSLog(@"Draw a rectangle at (%d %d %d %d) in %@",boods.x,boods.y,boods.width,boods.height,colorName(color)); return 0; }
然而在NSLog()中又调用了colorName()函数,此函数负责转换传入地颜色值,并且返回NSString值。
NSString *colorName (ShapeColor color) { switch (color) { case redColor: return @"Red"; break; case blueColor: return @"Blue"; break; case greenColor: return @"Green"; break; } }
这样子看起来很简单,也很显而易见。不过维护起来就有些难度了。比如我要加上一个drawTriangle()来绘制三角形,那么所有的函数基本上都要修改,而且有时还容易搞混。那么OOP就可以用来解决这些问题。
Objective-c 学习笔记(二)
时间: 2024-10-05 16:37:32