Objective-C邊學邊記-5:XCode源檔案組織

來源:互聯網
上載者:User

@class關鍵字
如要需要匯入某類的功能代碼則需要包含這個類的.h檔案,如果只需要知道這個東西是個類(如某檔案中如下聲明: Person *person; )使用@class就可以了(@class Person;)。正確的使用@class指令能夠減少編譯時間。

源檔案組織樣本:

 

 

////  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//  汽車引擎////  Created by Elf Sundae on 10-10-20.//  Copyright 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.//  Copyright 2010 __MyCompanyName__. All rights reserved.//#import <Cocoa/Cocoa.h>@class Engine;@class Tire;@interface Car : NSObject{Engine *engine;Tire *tires[4];}// 添加getter,setter- (Engine *) engine;- (void) setEngine:(Engine *) m_engine;- (Tire *) tireAtIndex: (int) index;//通過索引器訪問該屬性- (void) setTire: (Tire *) m_tire : (int) index;- (void) print;@end

 

 

////  Car.m//  car4////  Created by Elf Sundae on 10-10-20.//  Copyright 2010 __MyCompanyName__. All rights reserved.//#import "Car.h"#import "Engine.h"#import "Tire.h"@implementation Car//- (id) init// 初始化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"// 因為V8.h和WeatherRadial.h中#import "Tire.h"// 已經包含Engine.h和Tire.h,此處可不用匯入#import "Car.h"#import "V8.h"#import "WeatherRadial.h"/* * car4: 聯絡組織專案檔  * */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 ;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.