一聽標題,感覺十分的抽象。面向對象就是(Object-Oriented Programming)的首字母縮寫:OOP,是當今最主流的編程方法。
那么,面向對象編程具體有什么好處呢。是如何來實現呢?通過下面的實例,我想可以給大家最簡單直觀的理解了。
好了,下面就來說說我們這篇博文主要要介紹的內容吧。今天,我們通過一個繪制幾何圖形的實例來介紹基本的面向對象思想。
首先我們,新建一個Command Tools的命令行工具項目,選擇Foundation頭文件。
1 / 2 // main.m 3 // ch3_OOP_Shapes 4 // 5 // Created by pcbeta on 14-11-18. 6 // Copyright (c) 2014年 julian. All rights reserved. 7 // 面向對象的基本實例,繪制幾個幾何圖形 8 9 #import <Foundation/Foundation.h> 10 /* 1. enum 枚舉類型 */ 11 //定義繪制圖形的類型: 圓形,矩形,橢圓形 12 typedef enum{ 13 kCircle, 14 kRectangle, 15 kEgg 16 } ShapeType; 17 18 //定義繪制圖形的顏色: 紅色,綠色和藍色 19 typedef enum{ 20 kRedColor, 21 kGreenColor, 22 kBlueColor 23 } ShapeColor; 24 25 /* 2. struct 結構體 */ 26 //定義圖形的基本屬性 27 typedef struct{ 28 int x, y, width, height; 29 } ShapeRect; 30 31 //定義整體描述的形狀 32 typedef struct{ 33 ShapeType type; 34 ShapeColor color; 35 ShapeRect bounds; 36 } Shape;
如上面的代碼所示,我們定義了兩個枚舉類型的變量,和兩個結構圖的變量。接下來,我們在main主函數中聲明要繪畫的幾何圖形數組。
分別定義了一個紅色圓形,一個綠色矩形和一個藍色橢圓型。
1 // 定義三個幾何圖形的數組 2 Shape shapes[3]; 3 // 定義第一個幾何圖形為 紅色的圓形, 4 ShapeRect rect0 ={0,0,10,30}; 5 shapes[0].type = kCircle; 6 shapes[0].fillColor = kRedColor; 7 shapes[0].bounds = rect0; 8 9 // 定義第一個幾何圖形為 綠色的矩形, 10 ShapeRect rect1 ={30,40,50,60}; 11 shapes[1].type = kRectangle; 12 shapes[1].fillColor = kGreenColor; 13 shapes[1].bounds = rect1; 14 15 // 定義第一個幾何圖形為 藍色的橢圓形, 16 ShapeRect rect2 ={15,18,37,29}; 17 shapes[2].type = kEgg; 18 shapes[2].fillColor = kBlueColor; 19 shapes[2].bounds = rect2;
接下來,我們要定義每個幾個圖形的畫圖函數了。
1 /* 3.定義獲取顏色名稱的函數 */ 2 NSString *colorName (ShapeColor fillColor) 3 { 4 switch(fillColor) 5 { 6 case kRedColor: 7 return @"red"; 8 break; 9 case kGreenColor: 10 return @"green"; 11 break; 12 case kBlueColor: 13 return @"blue"; 14 break; 15 } 16 } 17 18 /* 4.定義幾何畫圖方法 */ 19 // 定義一個畫圓的方法 20 void drawCircle(ShapeRect bounds, ShapeColor fillColor) 21 { 22 NSLog(@"Drawing a circle at (%d %d %d %d) in %@", 23 bounds.x, 24 bounds.y, 25 bounds.height, 26 bounds.width, 27 colorName(fillColor)); 28 } 29 // 定義一個畫矩形的方法 30 void drawRectangle(ShapeRect bounds, ShapeColor fillColor) 31 { 32 NSLog(@"Drawing a rectangle at (%d %d %d %d) in %@", 33 bounds.x, 34 bounds.y, 35 bounds.height, 36 bounds.width, 37 colorName(fillColor)); 38 } 39 // 定義一個畫橢圓的方法 40 void drawEgg(ShapeRect bounds, ShapeColor fillColor) 41 { 42 NSLog(@"Drawing a egg at (%d %d %d %d) in %@", 43 bounds.x, 44 bounds.y, 45 bounds.height, 46 bounds.width, 47 colorName(fillColor)); 48 }
這時,我們可以再定義一個總的畫圖方法。可以循環輸出集合數組中不同類型的圖形。代碼如下:
1 //定義一個總的畫圖方法 2 void drawShape(Shape shapes[], int count) 3 { 4 for(int i=0; i<count; i++) 5 { 6 switch(shapes[i].type){ 7 case kCircle: 8 drawCircle(shapes[i].bounds, shapes[i].fillColor); 9 break; 10 case kRectangle: 11 drawRectangle(shapes[i].bounds, shapes[i].fillColor); 12 break; 13 case kEgg: 14 drawEgg(shapes[i].bounds, shapes[i].fillColor); 15 break; 16 } 17 } 18 }
最后,我們來寫main主函數。整合后,實現的代碼如下:
1 int main(int argc, const char * argv[]) 2 { 3 // 定義三個幾何圖形的數組 4 Shape shapes[3]; 5 // 定義第一個幾何圖形為 紅色的圓形, 6 ShapeRect rect0 ={0,0,10,30}; 7 shapes[0].type = kCircle; 8 shapes[0].fillColor = kRedColor; 9 shapes[0].bounds = rect0; 10 11 // 定義第二個幾何圖形為 綠色的矩形, 12 ShapeRect rect1 ={30,40,50,60}; 13 shapes[1].type = kRectangle; 14 shapes[1].fillColor = kGreenColor; 15 shapes[1].bounds = rect1; 16 17 // 定義第三個幾何圖形為 藍色的橢圓形, 18 ShapeRect rect2 ={15,18,37,29}; 19 shapes[2].type = kEgg; 20 shapes[2].fillColor = kBlueColor; 21 shapes[2].bounds = rect2; 22 23 drawShape(shapes, 3); 24 return 0; 25 }
運行后的結果如下:
好了,例子寫到這里。大家似乎看出點什么來了么?這里面的Shape其實就是一個簡單的對象。他有ShapeTpye, ShapeColor, ShapeRect 這些屬性。可以定義這個幾何圖形是什么類型,什么顏色,長寬高是什么。下篇博文,我們會仔細講解面向對象編程的方法和概念。