Listen to the title, feel very abstract. Object-oriented is the acronym for (object-oriented Programming): OOP, the most mainstream programming method today.
So what are the benefits of object-oriented programming? How is it implemented? Through the following example, I think we can give you the most simple and intuitive understanding.
Well, here's what we're going to be talking about in this blog post. Today, we introduce basic object-oriented thinking through an example of drawing geometry.
First, we create a command-line tool project that commands tools and select the foundation header file.
1/2//Main.m3//Ch3_oop_shapes4//5//Created by Pcbeta on 14-11-18.6//Copyright (c) 2014 Julian. All rights reserved.7//Basic object-oriented instances, drawing several geometries89#import <Foundation/Foundation.h>10/*1. Enum Enum type*/11//Define the type of drawing: round, rectangular, ovaltypedefEnum{13Kcircle,14Krectangle,15KEgg16} ShapeType;1718//Define the colors of the drawing: red, green, and bluetypedefEnum{20Kredcolor,21stKgreencolor,22Kbluecolor23} Shapecolor;2425/*2. struct Structure body*/26//Defining the basic properties of a graphic27 typedef struct{< Span style= "color: #008080;" >28 int x, y, width, height; Shaperect; 30 31 // Define the shape of the overall description 32 typedef struct {33 ShapeType type;< Span style= "color: #008080;" >34 Shapecolor color; Shaperect bounds; $ {} Shape;
As shown in the code above, we have defined two variables of the enumerated type, and two for the structure chart. Next, we declare the geometry array to be painted in the main function of main.
A red circle, a green rectangle, and a blue oval are defined respectively.
1//Defines an array of three geometries2 Shape shapes[3];3//Defines the first circle with a red geometry,4 Shaperect rect0 ={0,0,10,30};5 shapes[0].type =Kcircle;6 shapes[0].fillcolor =Kredcolor;7 shapes[0].bounds =Rect0;89//Defines the first rectangle with a green geometry,Ten Shaperect Rect1 ={30,40,40M60};shapes[1].type =Krectangle;shapes[1].fillcolor =Kgreencolor;13 shapes[1].bounds = Rect1; 14 15 // Defines the first ellipse with a blue shape, 16 shaperect rect2 ={15,18,37,29};17 shapes[2].type = Kegg; Shapes[2].fillcolor = Kbluecolor;19 shapes[2].bounds = Rect2;
Next, we'll define the drawing function for each of the several graphs.
1/*3. Define a function to get the name of a color*/2 NSString *ColorName (Shapecolor FillColor)3{4Switch(FillColor)5{6CaseKredcolor:7Return@"Red";8Break;9CaseKgreencolor:10Return@"Green";11Break;12CaseKbluecolor:13Return@"Blue";14Break;15}16}1718/*4. Defining geometric drawing methods*/19//Define a method for drawing circles20voidDrawcircle (shaperect bounds, Shapecolor fillcolor)21st{NSLog (@"Drawing a circle at (%d%d of%d) in%@",23Bounds.x,24Bounds.y,25Bounds.height,26Bounds.width,27ColorName (FillColor));28}29//Define a method for drawing rectangles30voidDrawRectangle (shaperect bounds, Shapecolor fillcolor)31{NSLog (@"Drawing a rectangle at (%d%d%d.%d) in%@",33Bounds.x,34Bounds.y,35Bounds.height,36Bounds.width,37ColorName (FillColor));38}39//Define a method for drawing ellipses40 void Drawegg ( Shaperect bounds, Shapecolor fillcolor) 41 {42 NSLog (@ "drawing a egg at (%d%d,%d) in%@ ",43 Bounds.x,44 Bounds.y,45 46 Bounds.width, 47 ColorName (FillColor)); 48}
At this point, we can define a general drawing method. You can loop through the output of different types of graphics in the collection array. The code is as follows:
1//Define a general drawing method2void Drawshape (Shape shapes[],IntCount3{4Forint i=0; i<count; i++)5{6Switch(Shapes[i].type) {7CaseKcircle:8Drawcircle (Shapes[i].bounds, Shapes[i].fillcolor);9Break10 case krectangle:< Span style= "color: #008080;" >11 DrawRectangle (shapes[i].bounds, Shapes[i].fillcolor); break; case Kegg:14 Drawegg (Shapes[i].bounds, Shapes[i].fillcolor); 15 break;16 }17 }18}
Finally, let's write the main function. After the integration, the following code is implemented:
1int main (int argc,Constchar *Argv[])2{3//Defines an array of three geometries4 Shape shapes[3];5//Defines the first circle with a red geometry,6 Shaperect rect0 ={0,0,10,30};7 shapes[0].type =Kcircle;8 shapes[0].fillcolor =Kredcolor;9 shapes[0].bounds =Rect0;1011//Defines a rectangle with a green shape for the second geometry,Shaperect Rect1 ={30,40,40M60};shapes[1].type =Krectangle;shapes[1].fillcolor =Kgreencolor;shapes[1].bounds =Rect1;1617//Defines the third ellipse with the blue geometry,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}
The results after the run are as follows:
Well, here's the example. Does everyone seem to see something? The shape in this is actually a simple object. He has Shapetpye, Shapecolor, shaperect these attributes. You can define what type this geometry is, what color it is, and what the length and width height is. In the next blog post, we will carefully explain the methods and concepts of object-oriented programming.
Wei iOS Basic Learning notes six OC Language Basics-01 Object oriented