The previous article demonstrated how to use the traditional "process-oriented programming method" to draw "rectangles", "Circles", and "ovans ", this article describes how to use the OOP method to implement it:
Because the two enumeration types "color" and "rectangular area" are used, extract them and put them in commdef. h file separately.
// Define the "ry color" enumeration typedef Enum {kredcolor, kgreencolor, kbluecolor} shapecolor; // define the" ry rectangle area" structure typedef struct {int X, Y, width, height;} shaperect;
Then define the base class shape,Note:In obj-C, defining a class is usually divided into two parts, one part is ". H "file, used to declare which members of the class (also known as the class definition file, similar to the interface), the other part is ". M file, used to provide specific implementation
The Declaration of the Shape class is as follows: (shape. h)
//// Shape. h // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "commdef. H "@ interface shape: nsobject {shapecolor fillcolor; shaperect bounds;}-(void) setfillcolor :( shapecolor) fillcolor;-(void) setbounds :( shaperect) bounds;-(void) draw;-(nsstring *) getcolorname :( shapecolor) fillcolor; @ end // shape
Shape class(Shape. m)
//// Shape. M // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "shape. H "@ implementation shape-(void) setbounds :( shaperect) B {bounds = B;}-(void) setfillcolor :( shapecolor) f {fillcolor = f;} // note: this method is left to the subclass for implementation, so here we only need an empty shell-(void) Draw {}-(nsstring *) getcolorname :( shapecolor) f {Switch (f) {Case kredcolor: Return @ "red"; break; Case kgreencolor: Return @ "green"; break; Case kbluecolor: Return @ "blue"; break; default: return @ "no clue"; break; }}@ end
The syntax is a bit strange. When I first came into contact, I had to force myself to forget it.
Then defineSubclass circle
Statement part circle. h
/// Circle. h // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "shape. H "// Note: Because the circle class does not need to extend other methods, there is only one shell @ interface circle: Shape {} @ end.
Implementation section circle. m
/// Circle. M // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "circle. H "@ implementation circle-(void) Draw {nslog (@" drawing a cirle at (% d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, [Super getcolorname: fillcolor]);} @ end
Note:The above shows how to call the method of the parent class in obj-C.[Super getcolorname: fillcolor]
Subclass rectangle
/// Rectangle. h // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "shape. H "@ interface rectangle: Shape {}@ end
Implementation
/// Rectangle. M // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "rectangle. H "@ implementation rectangle-(void) Draw {nslog (@" drawing a rectangle at (% d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, [Super getcolorname: fillcolor]);} @ end
Subclass Ellipse
//// Ellipse. h // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "shape. H "@ interface ellipse: Shape {}@ end
Implementation
//// Ellipse. M // helloworld /// created by Jimmy. yang on 11-1-26. // copy 2011 _ mycompanyname __. all rights reserved. // # import "ellipse. H "@ implementation ellipse-(void) Draw {nslog (@" drawing a ellipse at (% d, % d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, [Super getcolorname: fillcolor]);} @ end
Finally, let's take a look at the main function helloworld. M called.
# Import "commdef. H "# import" circle. H "# import" rectangle. H "# import" ellipse. H "int main (INT argc, const char * argv []) {ID shape [2]; shaperect rect0 = {0, 0, 10, 30 }; shape [0] = [circle new]; [shape [0] setbounds: rect0]; [shape [0] setfillcolor: kgreencolor]; [shape [0] draw]; shaperect rect1 = {0, 40, 50}; shape [1] = [rectangle new]; [shape [1] setbounds: rect1]; [shape [1] setfillcolor: kredcolor]; [shape [1] draw]; shaperect rect2 = {0, 30, 30}; shape [2] = [ellipse new]; [shape [2] setbounds: rect2]; [shape [2] setfillcolor: kbluecolor]; [shape [2] draw]; return 0 ;}
Note:The aboveCodeThere isIDIn obj-C, the ID is equivalent to "any type", meaning a pointer to an object (even if you do not know the object type). In short, if you do not know the specific type of an object, use it. We can also see that the"[Class New].
Attached file structure: