OBJECTIVE-C Basic Tutorial Study notes (eight)--split interface and implementation

Source: Internet
Author: User

In previous projects, our programming was written directly in a main.m file. The main () function of the class, @interface and @implementation parts are crammed into a file. This structure is also available for small programs and simple applications. But a lot of project documents, scale one up. This is not good, neither beautiful, code is not good management.

So, for the next blog post, let's go on to the previous section. Separate the code that he defines and implements, one class for each object. In objective-c, the defined file is written in the H file, and the implemented code is written in the M file. So let's start with a new command tools project.

Select where you want the project file to be saved

Then create a new three class object Car,engine,tire in the new project. Each class is built, generating two files (. h: Write definition method,. M: Write implementation method).

Then write the implementation method in ENGINE.M:

 1   #import   " engine.h   " 2  @ Implementation   Engine  4 -( NSString *) description  { return  (@ " i am an engine. vrooom!   "  7   8   @end  

Write implementation method in TIRE.M:

 1   #import   " tire.h   " 2  @ Implementation   Tire  4 -(nsstring *) description  5  {  Return  (@ " i am a tire. I last a while   " );  7   8   @end  

Next, modify the Car.h file to write the car class definition method:

1 #import<Foundation/Foundation.h>2 #import "Engine.h"3 #import "Tire.h"4 5 @interfaceCar:nsobject6 {7Engine *engine;8Tire *tires[4];//Four wheels, defines an array of four numbers. 9 }Ten-(Engine *) engine; One-(void) Setengine: (Engine *) Newengine; A-(Tire *) Tireatindex: (int) index; --(void) Settire: (Tire *) Tire Atindex: (int) index; --(void) Drive; the @end //Car

Note: You should first refer to "Engine.h" and "Tire.h" at the top. Otherwise, the code will not compile through.

Then you modify the implementation of the car class:

1 #import "Car.h"2 3 @implementationCar4-(void) Setengine: (Engine *) Newengine5 {6Engine =Newengine;7 }8 9-(Engine *) EngineTen { One     return(engine); A } -  --(void) Settire: (Tire *) Tire theAtindex: (int) Index - { -     if(index<0|| Index>3) -     { +NSLog (@"Bad Index (%d) in Settire:atindex", - index); +Exit1); A     } atTires[index] =Tire; - } -  --(Tire *) Tireatindex: (int) Index - { -     if(index<0|| Index>3) in     { -NSLog (@"Bad Index (%d) in Tireatindex:", to index); +Exit1); -     } the     return(Tires[index]); * } $ Panax Notoginseng-(void) drive{ -NSLog (@"%@", engine); theNSLog (@"%@", tires[0]); +NSLog (@"%@", tires[1]); ANSLog (@"%@", tires[2]); theNSLog (@"%@", tires[3]); + } - @end

Finally, as long as in the main function of the main functions of the file, reference three class file definition file can be, the implementation of the Code does not change:

1 #import<Foundation/Foundation.h>2 #import "Engine.h"3 #import "Tire.h"4 #import "Car.h"5 6 intMainintargcConst Char*argv[])7 {8Car *car = [carNew];9Engine *engine = [engineNew];Ten [car setengine:engine]; One      for(intI=0;i<4; i++) A     { -Tire *tire = [TireNew]; - [car settire:tire atindex:i]; the     } -      - [car drive]; -     return 0; +}

The basic structure of the project file and the results of the run

Since this blog post is simply a re-collation of the code structure, as for the function code is the same as described in the previous article, this is no longer elaborate. If your friends are interested or don't understand, you can check the previous blog post or leave a message for me. Well, the time is late, wash and go to bed early.

OBJECTIVE-C Basic Tutorial Study notes (eight)--split interface and implementation

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.