標籤:物件導向編程 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;
接下來就是在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就可以用來解決這些問題。