1.过程式编程实例,画出Shape数组中的图形:
//
// main.m
// hello-obj
//
// Created by zhouyang on 16/4/4.
// Copyright © 2016年 zhouyang. All rights reserved.
//
#import <Foundation/Foundation.h>
//枚举变量,代表形状
typedef enum{
kCircle,
kRectangle,
kEgg
} ShapeType;
//枚举变量,代表颜色
typedef enum{
kRedColor,
kGreenColor,
kBlueColor
} ShapeColor;
//结构体,代表形状
typedef struct{
int x,y,width,height;
}ShapeRect;
//结构体,表示多边形的各项属性
typedef struct {
ShapeType type;
ShapeColor fillColor;
ShapeRect bounds; }Shape;
//返回颜色对应的NSString
NSString *colorName(ShapeColor colorName){
switch(colorName){
case kRedColor:
return @"red";
break;
case kGreenColor:
return @"green";
break;
case kBlueColor:
return @"blue";
break;
}
return @"no clue";
}
//“画出”圆,其实就是输出图形的各项属性
void drawCircle(ShapeRect bounds,ShapeColor fillColor){
NSLog(@"drawing a circle at (%d,%d,%d,%d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
colorName(fillColor)
);
}
//“画出”长方形
void drawRectangle(ShapeRect bounds,ShapeColor fillColor){
NSLog(@"drawing a rectangle at (%d,%d,%d,%d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
colorName(fillColor)
);
}
//"画出"Egg
void drawEgg(ShapeRect bounds,ShapeColor fillColor){
NSLog(@"drawing a egg at (%d,%d,%d,%d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
colorName(fillColor)
);
}
//遍历Shape数组,画出对应的图形
void drawShapes(Shape shapes[],int count){
for(int i=;i<count;i++){
switch (shapes[i].type) {
case kCircle:
drawCircle(shapes[i].bounds, shapes[i].fillColor);
break;
case kRectangle:
drawRectangle(shapes[i].bounds,shapes[i].fillColor);
break;
case kEgg:
drawEgg(shapes[i].bounds,shapes[i].fillColor);
break; default:
NSLog(@"can't find the shape!");
} }
}
int main(int argc, const char * argv[]) {
Shape shapes[];
//Circle
ShapeRect rect0={,,,};
shapes[].type=kCircle;
shapes[].fillColor=kRedColor;
shapes[].bounds=rect0;
//Rectangle
ShapeRect rect1={,,,};
shapes[].type=kRectangle;
shapes[].fillColor=kGreenColor;
shapes[].bounds=rect1;
//Egg
ShapeRect rect2={,,,};
shapes[].type=kEgg;
shapes[].fillColor=kBlueColor;
shapes[].bounds=rect2;
//画出数组Shape中的图形
drawShapes(shapes,);
}
基于OOP编程,可以将上述代码改写为以下格式:
#import <Foundation/Foundation.h>
//注意枚举变量的定义,变量之间以逗号隔开
typedef enum{
kRedColor,
kGreenColor,
kBlueColor
}ShapeColor;
typedef struct {
int x,y,width,height;
}ShapeRect;
NSString *ColorName(ShapeColor color){
switch (color) {
case kRedColor:
return @"red";
break;
case kGreenColor:
return @"green";
break;
case kBlueColor:
return @"blue";
break;
}
return @"no clue";
}
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Circle:NSObject{
//定义数据成员
@private
ShapeColor fillColor;
ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Circle
//接口定义完毕
//下面开始实现接口Circle
@implementation Circle
- (void) setFillColor:(ShapeColor) c {
fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
bounds=b;
}
- (void) draw{
NSLog(@"drawing a circle at (%d %d %d %d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
ColorName(fillColor)
);
}
@end //Circle
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Rectangle:NSObject{
//定义数据成员
@private
ShapeColor fillColor;
ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Rectangle
//接口定义完毕
//下面开始实现接口Rectangle
@implementation Rectangle
- (void) setFillColor:(ShapeColor) c {
fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
bounds=b;
}
- (void) draw{
NSLog(@"drawing a rectangle at (%d %d %d %d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
ColorName(fillColor)
);
}
@end //Egg
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Egg:NSObject{
//定义数据成员
@private
ShapeColor fillColor;
ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Triangle
//接口定义完毕
//下面开始实现接口Triangle
@implementation Egg
- (void) setFillColor:(ShapeColor) c {
fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
bounds=b;
}
- (void) draw{
NSLog(@"drawing a egg at (%d %d %d %d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
ColorName(fillColor)
);
}
@end //Egg
//定义接口
//@告诉编译器,这里是对C的扩展
@interface Triangle:NSObject{
//定义数据成员
@private
ShapeColor fillColor;
ShapeRect bounds;
}
//列举方法的名称,返回值的类型和某些参数
//前面的短线表明这是Objective-c方法的声明
- (void) setFillColor:(ShapeColor) fillColor;
- (void) setBounds:(ShapeRect) bounds;
- (void) draw;
@end //@Triangle
//接口定义完毕
//下面开始实现接口Triangle
@implementation Triangle
- (void) setFillColor:(ShapeColor) c {
fillColor=c;
}//设置颜色
- (void) setBounds:(ShapeRect) b{
bounds=b;
}
- (void) draw{
NSLog(@"drawing a triangle at (%d %d %d %d) in %@",
bounds.x,
bounds.y,
bounds.width,
bounds.height,
ColorName(fillColor)
);
}
@end //Triangle
//画出图形
//id 对应类实例
void drawShapes(id shapes[],int count){
for(int i=;i<count;i++){
id shape=shapes[i];
[shape draw];
}
}
int main(int argc, const char * argv[]) { id shapes[];
//Circle
shapes[]=[Circle new];//这里创建新的类实例将new加在类名称的后面
ShapeRect rect0={,,,};
[shapes[] setBounds:rect0];
[shapes[] setFillColor:kRedColor];
//Rectangle
ShapeRect rect1={,,,};
shapes[] =[ Rectangle new];
[shapes[] setBounds:rect1];
[shapes[] setFillColor:kGreenColor];
//Egg
ShapeRect rect2={,,,};
shapes[] =[Egg new];
[shapes[] setBounds:rect2];
[shapes[] setFillColor:kBlueColor];
//Triangle
ShapeRect rect3 ={,,,};
shapes[] =[Triangle new];
[shapes[] setBounds:rect3];
[shapes[] setFillColor:kGreenColor];
//画出数组Shape中的图形
drawShapes(shapes,);
return ;
}
可见利用OOP编程方式,增加新的图形更加简单,修改的地方更少。
请注意object-c中的接口,类的定义以及类方法的调用,这里与C++还是存在很多的不同。
可能会遇到错误:Must explicitly describe intended ownership of an object array parameter,解决方法如下:
将Objective-c Automatic Reference Counting 设置为No,关闭ARC即可。