@ Class keywords
To import a certain type of functional code, you need to include this class. h file. If you only need to know that this object is a class (for example, the following declaration in a file: person * person;), you can use @ class person (@ class person ;). Correct use of the @ Class command can reduce the Compilation Time.
Example of source file organization:
//// Tire.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@interface Tire : NSObject@end
//// Tire.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Tire.h"@implementation Tire- (NSString *) description{return(@"I am a Tire");}@end
/// Engine. h // Auto Engine // created by Elf sundae on 10-10-20. // copy 2010 _ mycompanyname __. all rights reserved. // # import <Cocoa/cocoa. h> @ Interface Engine: nsobject @ end
//// Engine.m// ////// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Engine.h"@implementation Engine- (NSString *) description{return (@"I am a Engine");}@end
//// V8.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>#import "Engine.h"// *******************// V8 Engine Class@interface V8 : Engine@end
//// V8.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "V8.h"@implementation V8- (NSString *) description{return @"I am a V8 Engine!";}@end // V8
//// Car. h // car4 // created by Elf sundae on 10-10-20. // copy 2010 _ mycompanyname __. all rights reserved. // # import <Cocoa/cocoa. h> @ Class engine; @ Class tire; @ interface car: nsobject {Engine * engine; tire * tires [4];} // Add getter, setter-(Engine *) engine;-(void) setengine :( engine *) m_engine;-(tire *) tireatindex: (INT) index; // access this property through the indexer-(void) settire: (tire *) m_tire: (INT) index;-(void) print; @ end
//// Car. M // car4 // created by Elf sundae on 10-10-20. // copy 2010 _ mycompanyname __. all rights reserved. // # import "car. H "# import" engine. H "# import" tire. H "@ implementation car //-(ID) Init // initialize the car // {// If (Self = [Super init]) {// engine = [engine new]; /// tires [0] = [Tire new]; // tires [1] = [Tire new]; // tires [2] = [Tire new]; // tires [3] = [Tire new]; //} // return self; //}-(Engine *) engine {return engine;}-(void) setengine :( engine *) m_engine {Engine = m_engine;}-(tire *) tireatindex :( INT) index {If (index <0 | index> 3) {nslog (@ "Bad index (% d) in \" tireatindex: \ "", index); exit (1) ;}return tires [Index] ;}- (void) settire :( tire *) m_tire :( INT) index {If (index <0 | index> 3) {nslog (@ "Bad index (% d) in \" settire: atindex \ "", index); exit (1);} tires [Index] = m_tire;}-(void) print {nslog (@ "% @", engine ); nslog (@ "% @", tires [0]); nslog (@ "% @", tires [1]); nslog (@ "% @", tires [2]); nslog (@ "% @", tires [3]);} @ end
//// WeatherRadial.h// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>#import "Tire.h"// *******************// WeatherRadial Tire@interface WeatherRadial : Tire@end
//// WeatherRadial.m// car4//// Created by Elf Sundae on 10-10-20.// Copyright 2010 __MyCompanyName__. All rights reserved.//#import "WeatherRadial.h"@implementation WeatherRadial- (NSString *) description{return (@"I am a WeatherRadial Tire!!");}@end
# Import <Cocoa/cocoa. h> # import "engine. H "// because v8.h and weatherradial. # import "tire. H "// contains the engine. H and tire. h. You do not need to import # import "car. H "# import" v8.h "# import" weatherradial. H "/** car4: contact the organization project file **/INT main (INT argc, const char * argv []) {car * car; car = [Car new]; v8 * Engine = [V8 new]; [Car setengine: Engine]; int I; for (I = 0; I <4; I ++) {weatherradial * tire = [weatherradial new]; [Car settire: Tire: I];} [Car print]; return 0 ;}