iOS面向过程,面向对象一则实例

//面向过程编程:其中有iOS枚举和结构实例,这段能很好解释iOS中的结构和枚举:

#import <Foundation/Foundation.h>

// --------------------------------------------------
// constants for the different kinds of shapes and their colors

typedef enum {
	kCircle,
	kRectangle,
	kOblateSpheroid
} ShapeType;


typedef enum {
	kRedColor,
	kGreenColor,
	kBlueColor
} ShapeColor;


// --------------------------------------------------
// Shape bounding rectangle

typedef struct {
	int x, y, width, height;
} ShapeRect;


// --------------------------------------------------
// The Shape

typedef struct {
	ShapeType type;
	ShapeColor fillColor;
	ShapeRect bounds;
} Shape;


// --------------------------------------------------
// convert from the ShapeColor enum value to a human-readable name

NSString *colorName (ShapeColor colorName)
{
  switch (colorName) {
  case kRedColor:
      return @"red";
      break;
  case kGreenColor:
      return @"green";
      break;
  case kBlueColor:
      return @"blue";
      break;
  }

  return @"no clue";

} // colorName


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));
	
} // drawCircle


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));
	
} // drawRectangle


void drawEgg (ShapeRect bounds, 
			  ShapeColor fillColor)
{
	NSLog (@"drawing an egg at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
	
} // drawEgg


// --------------------------------------------------
// Draw the shapes

void drawShapes (Shape shapes[], int count)
{
	int i;
	
	for (i = 0; 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 kOblateSpheroid:
				drawEgg (shapes[i].bounds, 
						 shapes[i].fillColor);
				break;
		}
	}
	
} // drawShapes


// --------------------------------------------------
// The main function.  Make the shapes and draw them

int main (int argc, const char * argv[]) 
{
	Shape shapes[3];
	
	ShapeRect rect0 = { 0, 0, 10, 30 };
	shapes[0].type = kCircle;
	shapes[0].fillColor = kRedColor;
	shapes[0].bounds = rect0;
	
	ShapeRect rect1 = { 30, 40, 50, 60 };
	shapes[1].type = kRectangle;
	shapes[1].fillColor = kGreenColor;
	shapes[1].bounds = rect1;
	
	ShapeRect rect2 = { 15, 18, 37, 29 };
	shapes[2].type = kOblateSpheroid;
	shapes[2].fillColor = kBlueColor;
	shapes[2].bounds = rect2;
	
	drawShapes (shapes, 3);
	
	return (0);
	
} // main

面向对象的编程:

#import <Foundation/Foundation.h>

// --------------------------------------------------
// constants for the different kinds of shapes and their colors

typedef enum {
	kRedColor,
	kGreenColor,
	kBlueColor
} ShapeColor;


// --------------------------------------------------
// Shape bounding rectangle


typedef struct {
	int x, y, width, height;
} ShapeRect;


// --------------------------------------------------
// convert from the ShapeColor enum value to a human-readable name

NSString *colorName (ShapeColor color)
{
	NSString *colorName;
	
	switch (color) {
		case kRedColor:
			colorName = @"red";
			break;
		case kGreenColor:
			colorName = @"green";
			break;
		case kBlueColor:
			colorName = @"blue";
			break;
		default:
			colorName = @"oops! Unexpected color";
			break;
	}
	
	return (colorName);
	
} // colorName


// --------------------------------------------------
// Shapes base class

@interface Shape : NSObject
{
	ShapeColor  fillColor;
	ShapeRect   bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;

@end // Shape


@implementation Shape

- (void) setFillColor: (ShapeColor) c
{
	fillColor = c;
} // setFillColor

- (void) setBounds: (ShapeRect) b
{
	bounds = b;
} // setBounds

- (void) draw
{
} // draw

@end // Shape




// --------------------------------------------------
// All about Triangles

@interface Triangle : Shape
@end // Triangle


@implementation Triangle

- (void) draw
{
	NSLog (@"drawing a triangle at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
} // draw

@end // Triangle




// --------------------------------------------------
// All about Circles

@interface Circle : Shape

@end // Circle


@implementation Circle

// I'm new!
- (void) setFillColor: (ShapeColor) c
{
	if (c == kRedColor) {
		c = kGreenColor;
	}
	
	[super setFillColor: c];
	
} // setFillColor


- (void) draw
{
	NSLog (@"drawing a circle at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
} // draw

@end // Circle




// --------------------------------------------------
// All about Rectangles

@interface Rectangle : Shape

@end // Rectangle


@implementation Rectangle

- (void) draw
{
	NSLog (@"drawing a rectangle at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
} // draw

@end // Rectangle


// --------------------------------------------------
// All about OblateSphereoids

@interface OblateSphereoid : Shape

@end // OblateSphereoid


@implementation OblateSphereoid

- (void) draw
{
	NSLog (@"drawing an egg at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
} // draw

@end // OblateSphereoid



// --------------------------------------------------
// Draw the shapes

void drawShapes (id shapes[], int count)
{
	int i;
	
	for (i = 0; i < count; i++) {
		id shape = shapes[i];
		[shape draw];
	}
	
} // drawShapes



// --------------------------------------------------
// The main function.  Make the shapes and draw them

int main (int argc, const char * argv[]) 
{
	id shapes[4]; // I'm different!
	
	ShapeRect rect0 = { 0, 0, 10, 30 };
	shapes[0] = [Circle new];
	[shapes[0] setBounds: rect0];
	[shapes[0] setFillColor: kRedColor];
	
	ShapeRect rect1 = { 30, 40, 50, 60 };
	shapes[1] = [Rectangle new];
	[shapes[1] setBounds: rect1];
	[shapes[1] setFillColor: kGreenColor];
	
	ShapeRect rect2 = { 15, 19, 37, 29 };
	shapes[2] = [OblateSphereoid new];
	[shapes[2] setBounds: rect2];
	[shapes[2] setFillColor: kBlueColor];
	
	ShapeRect rect3 = { 47, 32, 80, 50 };	// I'm new!
	shapes[3] = [Triangle new];
	[shapes[3] setBounds: rect3];
	[shapes[3] setFillColor: kRedColor];
	
	drawShapes (shapes, 4); // I'm different!
	
	return (0);
	
} // main


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值