Objective-c Study Notes (2)

Source: Internet
Author: User

Object-Oriented Programming (1)


Process-oriented programming


C language is a process-oriented programming language. Let's take a piece of program code to gain a deeper understanding of process-oriented.

Draw a set chart:

//// 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) 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) 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) 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 ;}
The running result of the program is as follows:



These functions are analyzed as follows:

First, use enumeration to specify several shapes that can be drawn.

typedef enum {    circle,    rectangle,    egg} ShapeType;

Next we will enumerate several colors that can be filled

typedef enum {    redColor,    greenColor,    blueColor} ShapeColor;

Then let's set the area to be drawn.

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

Finally, a struct is used to combine the preceding content to describe a shape as a whole.

typedef struct {    ShapeType type;    ShapeColor color;    ShapeRect boods;} Shape;

The next step is to declare three shapes and various attribute values in three shapes in the main () function.

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;}

The drawshapes () function is called in the main () function to draw graphs.

The drawshapes () function line cyclically checks the shape structure in each array, then views the type field with the switch, and calls an appropriate function to draw a graph.

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;        }    }}

At this time, three functions, drawcircle (), drawrect (), and drawegg, are called. These functions need to output the shape information and pass it to the local color.

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;}

However, the colorname () function is called in nslog (). This function is used to convert the incoming color value and return the nsstring value.

NSString *colorName (ShapeColor color) {    switch (color) {        case redColor:            return @"Red";            break;        case blueColor:            return @"Blue";            break;        case greenColor:            return @"Green";            break;    }}



This looks simple and obvious. However, maintenance is difficult. For example, if I want to add a drawtriangle () to draw a triangle, then all the functions are basically modified and sometimes easy to mix. Then OOP can be used to solve these problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.