面向对象与面向过程的区别
面向过程:
它是一种自顶向下的编程,把问题分成几步,解决问题时,一步一步的调用,过程式程序建立在函数之上,数据为函数服务。
#import <Foundation/Foundation.h>
typedef enum {
kCircle,
kRectangle,
kEgg
} ShapeType;
typedef enum {
kRedColor,
kGreenColor,
kBlueColor
} ShapeColor;
typedef struct {
int x;
int y;
int width;
int height;
} ShapeRect;
typedef struct {
ShapeType type;
ShapeColor color;
ShapeRect bounds;
} Shape;
void drawShapes(Shape *sha, int number);
int main(int argc, const char * argv[])
{
// 定义一个形状的数组,有3个成员
Shape shapes[3] = {0};
// 给第0个成员赋值
ShapeRect rectCircle = {0, 0, 20, 20};
shapes[0].bounds = rectCircle;
shapes[0].type = kCircle;
shapes[0].color = kRedColor;
// 给第1个成员赋值
ShapeRect rectRect = {40, 40, 100, 50};
shapes[1].bounds = rectRect;
shapes[1].type = kRectangle;
shapes[1].color = kGreenColor;
// 给第2个成员赋值
ShapeRect rectEgg = {100, 90, 80, 100};
shapes[2].bounds = rectEgg;
shapes[2].type = kEgg;
shapes[2].color = kBlueColor;
drawShapes(shapes, 3);
return 0;
}
NSString * colorName(ShapeColor color)
{
NSString *name =nil;
switch (color) {
case kRedColor:
name = @"Red";
break;
case kGreenColor:
name = @"Green";
break;
case kBlueColor:
name = @"Blue";
break;
default:
break;
}
return name;
}
void drawCircle(ShapeRect rect, ShapeColor color)
{
NSLog(@"Draw Circle at [%d,%d,%d,%d], fill color is %@", rect.x, rect.y, rect.width, rect.height, colorName(color));
}
void drawRectangle(ShapeRect rect, ShapeColor color)
{
NSLog(@"Draw rectangle at [%d,%d,%d,%d], fill color is %@", rect.x, rect.y, rect.width, rect.height, colorName(color));
}
void drawEgg(ShapeRect rect, ShapeColor color)
{
NSLog(@"Draw egg at [%d,%d,%d,%d], fill color is %@", rect.x, rect.y, rect.width, rect.height, colorName(color));
}
void drawShapes(Shape *sha, int number)
{
int i = 0;
for (i = 0; i < number; i++) {
// 绘制每一个形状
switch (sha[i].type) {
case kCircle:
drawCircle(sha[i].bounds, sha[i].color);
break;
case kRectangle:
drawRectangle(sha[i].bounds, sha[i].color);
break;
case kEgg:
drawEgg(sha[i].bounds, sha[i].color);
break;
default:
break;
}
}
}
面向对象:
面向对象是把问题分解成个个对象,在各个对象中有自己的行为,面向对象的思想是依程序的数据为中心,函数为数据服务。
先建立一个头文件#impore <common.h>
#ifndef DrawShapOOP_ShapeData_h
#define DrawShapOOP_ShapeData_h
// 形状类型
typedef enum {
kCircle,
kRectangle,
kEgg
} ShapeType;
// 填充颜色
typedef enum {
kRedColr,
kGreenColor,
kBlueColor
} ShapeColor;
// 形状的区域(边界)
typedef struct {
int x;
int y;
int width;
int height;
} ShapeRect;
#endif
建三个类,类名分别为:Circle.h, Rectangle.h,Egg.h这三个类的.h里声明里的成员变量和声明方法都一样:
@interface Rectangle : NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}
-(void)setFillColor:(ShapeColor)color;
-(void)setBounds:(ShapeRect)bound;
-(void)draw;
@end
它们的.m里放的是实现方法(三个.m都一样的)
@implementation Egg
-(void)setFillColor:(ShapeColor)color
{
fillColor = color;
}
-(void)setBounds:(ShapeRect)bound
{
bounds = bound;
}
-(NSString *)colorName
{
NSString *name = nil;
switch (fillColor) {
case KRed:
name = @"Red";
break;
case KGreen:
name = @"Green";
break;
case KBlue:
name = @"Blue";
break;
default:
name = @"noColor";
break;
}
return name;
}
-(void)draw
{
NSLog(@"draw Egg at %d,%d,%d,%d,%@",bounds.x,bounds.y,bounds.width,bounds.height,[self colorName]);
}
@end
main.m里的代码为:
#import <Foundation/Foundation.h>
#import "Circle.h"
#import "Rectangle.h"
#import "Egg.h"
#import "QYShape.h"
void drawShpaes(id shap[3],int count);
int main(int argc, const char * argv[])
{
// 创建一个数组,用于存放具体的图形对象
//QYShape *shapes[3];
id shapes[3];
// 创建Circle对象
Circle *circle = [Circle new];
[circle setFillColor:kRed];
ShapeRect bound = {0,0,20,40};
[circle setBounds:bound];
// 将circle对象放到数组的第一个元素位置
shapes[0] = circle;
// 创建Rectangle对象
Rectangle *rectangle = [Rectangle new];
[rectangle setFillColor:kGreen];
ShapeRect rectBound = {0,100,30,50};
[rectangle setBounds:rectBound];
// 将rectangle对象放到数组的第二个元素位置
shapes[1] = rectangle;
// 创建Egg对象
Egg *eg = [Egg new];
[eg setFillColor:kBlue];
ShapeRect eggBound = {100,200,60,80};
[eg setBounds:eggBound];
// 将eg对象放到数组第三个元素的位置
shapes[2] = eg;
// 调用绘制图形的方法
drawShpaes(shapes, 3);
return 0;
}
//画图的总控方法
void drawShpaes(QYShape* shap[3],int count)
{
for (int i = 0; i < count; i++) {
[shap[i] draw];
}
}