2012-05-26 52 views
0

Rectangle的實現部分,當我遺漏了#import "XYpoint"它仍然對我一樣。是否將#import "XYpoint"良好做法或它影響程序?是否需要在此處使用#import?

#import <Foundation/Foundation.h> 

@interface XYPoint : NSObject 
@property int x, y; 

-(void) setX: (int) xVar andY: (int) yVar; 

@end 

#import "XYpoint.h" 

@implementation XYPoint 
@synthesize x, y; 

-(void) setX:(int)xVar andY:(int)yVar { 
    x = xVar; 
    y = yVar; 
} 

@end 

#import <Foundation/Foundation.h> 

    @class XYPoint; 
    @interface Rectangle: NSObject 

    -(XYPoint *) origin; 
    -(void) setOrigin: (XYPoint *) pt; 
@end 

#import "Rectangle.h" 
#import "XYpoint.h" 

@implementation Rectangle { 
    XYPoint *origin; 
} 

-(void) setOrigin:(XYPoint *)pt { 
    origin = pt; 
} 
-(XYPoint *) origin { 
    return origin; 
} 

@end 

#import "XYpoint.h" 
#import "Rectangle.h" 

int main (int argc, char * argv[]) { 
    @autoreleasepool { 
     Rectangle *rect = [[Rectangle alloc] init]; 
     XYPoint *pointy = [[XYPoint alloc] init]; 

     [pointy setX:5 andY:2]; 
     rect.origin = pointy; 

     NSLog(@"Origin %i %i", rect.origin.x, rect.origin.y); 
    } 
    return 0; 
} 

回答

3

你實現Rectangle不使用XYPoint類的任何細節。它只是將其視爲一個通用指針,並且不會將其消息或解除引用。因此,前向聲明(在Rectangle接口文件中的@class聲明)就足夠了。導入標題對編譯的程序沒有任何影響。

很有可能您的課程最終會發展爲關注XYPoint課程的界面。當它這樣做時,它將需要導入該接口聲明。如果你忽略導入它,編譯器會警告你。

這就是說,沒有理由不導入它。

相關問題