[Objective-C basic tutorial note ch03] (4) oop in oC

Source: Internet
Author: User

1. Steps for declaring a class interface:

1. Declare a class interface and add the class name with @ interfacekeyword.

2. Use {instance variable} to define various data members.

3. method declaration: declare a C function using the infix syntax and use the colon :.


Ii. Declaration class interface instance:

// Declare the circular circle class interface @ interface circle: nsobject {shapecolor fillcolor; // after each new circle object is created, the object contains the two elements-class instance variable shaperect bounds;} // specify the instance variable-(void) setfillcolor: (shapecolor) fillcolor; // method declaration, infix-(void) setbounds: (shaperect) bounds;-(void) draw; @ end // statement of the circle completion class


Iii. Implementation steps

1. Use [email protected] to add the class name

2. Compile the method body {method body} For the method declared in the implementation class }.


IV. Implementation instances


@ Implementation circle // class implementation-(void) setfillcolor: (shapecolor) c {fillcolor = C ;}// setfillcolor-(void) setbounds: (shaperect) B {bounds = B;} // setbounds-(void) Draw {nslog (@ "drawing a circle at (% d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // Implementation of the draw @ end // circle completion class

5. Case study-geometric drawing and fill color

//// Main. M // shapes-object // created by Jason on 14-6-10. // copyright (c) 2014 jasonapp. all rights reserved. // # import <Foundation/Foundation. h> // define different color typedef Enum {kredcolor, kgreencolor, kbluecolor} shapecolor; // define a rectangle to specify the drawing area typedef struct {int x, y, width, height;} shaperect; // converts the incoming color value and returns the nsstring literal nsstring * colorname (shapecolor color) {Switch (color) {Case kredcolor: return @ "red"; break; Case kgreencolor: Return @ "green"; break; Case kbluecolor: Return @ "blue"; break;} return @ "no clue ";} // colorname // declare the circular circle class interface @ interface circle: nsobject {shapecolor fillcolor; // after each new circle object is created, the object contains the two elements-class instance variable shaperect bounds;} // specify the instance variable-(void) setfillcolor: (shapecolor) fillcolor; // method declaration, infix-(void) setbounds: (shaperect) bounds;-(void) draw; @ end // The Declaration of the circle completion class @ implementation circle // class implementation-(void) setfillcolor: (shapecolor) c {fillcolor = C;} // setfillcolor-(void) setbounds: (shaperect) B {bounds = B;} // setbounds-(void) Draw {nslog (@ "drawing a circle at (% d) in % @ ", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // draw @ end // Implementation of the circle completion class // defines the rectangle @ interface rectangle: nsobject {shapecolorfillcolor; shaperectbounds;}-(void) setfillcolor: (shapecolor) fillcolor;-(void) setbounds: (shaperect) bounds;-(void) draw; @ end // rectangle @ implementation rectangle-(void) setfillcolor: (shapecolor) c {fillcolor = C;} // setfillcolor-(void) setbounds: (shaperect) B {bounds = B;} // setbounds-(void) draw {nslog (@ "drawing a rectangle at (% d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // draw @ end // rectangle // define oblatesphereoids @ interface identifier: nsobject {shapecolorfillcolor; shaperectbounds;}-(void) setfillcolor: (shapecolor) fillcolor;-(void) setbounds: (shaperect) bounds;-(void) draw; @ end // signature @ implementation oblatesphereoid-(void) setfillcolor: (shapecolor) c {fillcolor = C;} // setfillcolor-(void) setbounds: (shaperect) B {bounds = B;} // setbounds-(void) draw {nslog (@ "drawing an egg at (% d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // draw @ end // oblatesphereoid // defines the triangle @ interface triangle: nsobject {shapecolorfillcolor; shaperectbounds;}-(void) setfillcolor: (shapecolor) fillcolor;-(void) setbounds: (shaperect) bounds;-(void) draw; @ end // triangle @ implementation triangle-(void) setfillcolor: (shapecolor) c {fillcolor = C;} // setfillcolor-(void) setbounds: (shaperect) B {bounds = B;} // setbounds-(void) draw {nslog (@ "drawing a triangle at (% d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorname (fillcolor);} // draw @ end // triangle // draw the void drawshapes (ID shapes [], int count) {for (INT I = 0; I <count; I ++) {ID shape = shapes [I]; [shape draw] ;}// drawshapesint main (INT argc, const char * argv []) {ID shapes [4]; 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 }; shapes [3] = [triangle new]; [shapes [3] setbounds: rect3]; [shapes [3] setfillcolor: kredcolor]; drawshapes (shapes, 4 ); return (0 );}



Vi. Summary

1. Procedural programming-"function first, data second"

2. Oop -- "data first, function second"

3. "open/close principle"-the software entity should be open to expansion and closed to changes

4. The private method does not exist in objective-C.

5. Objective-C is responsible for executing important tasks, such as sending messages to objects and passing the number of workers, to support application execution.

6. instantiate an object-send a new message to the corresponding class to create an object.




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.