标签:面向对象编程 xcode objective-c iphone
面向对象编程(一)
面向过程编程
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;
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; }
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 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; }
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 学习笔记(二),布布扣,bubuko.com
标签:面向对象编程 xcode objective-c iphone
原文地址:http://blog.csdn.net/jnnock/article/details/38433763