2013-03-17 2 views
-2
@interface Rectangle: NSObject 
-(void) setOrigin : (XYPoint *) pt; 
-(XYPoint *) origin; 
... 
@end 

#import "XYPoint.h" 
#import "Rectangle.h" 
@implementation Rectangle 
-(void) setOrigin : (XYPoint *) pt 
{ 
    origin = pt; 
} 

-(XYPoint *) origin 
{ 
    return origin; 
} 
@end 

@interface XYPoint: NSObject 
@property int x, y; 
-(void) setX: (int) xVall andY; (int) yVal; 
@end 

#import "XYPoint.h" 
@implementation XYPoint 
@synthesize x, y 
-(void) setX: (int) xVal andY: (int) yVal 
{ 
    x = xVal; 
    y = yVal; 
} 
@end 

위는 클래스의 일부이다. 이것은 기본입니다나는 방법의 차이를 알 수 없다.

int main (int argc, const char* argv[]) 
{ 
    XYPoint* myPoint = [[XYPoint alloc]init]; 
    Rectangle* myRect = [[Rectangle alloc]init]; 

    [myPoint setX: 200 andY: 100]; 

    [myRect setOrigin: myPoint]; 
    NSLog(@"origin of rectangle: (%i, %i)", myRect.origin.x, myRect.origin.y); 
    return 0; 
} 

예상대로 잘 작동합니다. 제가

-(void) setOrigin: (XYPoint *) pt 
{ 
    origin.x = pt.x; 
    origin.y = pt.y; 
} 

에있어서 직사각형 클래스

setOrigin: (XYPoint *) pt 

을 변경할 경우에, 단지 X와 Y 모두 0의 값을 출력한다. 이 두 가지 방법의 차이점은 무엇입니까? 나를 위해, 그것은 같아 보인다.

+0

당신이 너무'XYPoint'와'Rectangle'의'@의 interface'를 제공 할 수 있습니다 ... 당신이하지 allocRectangle 클래스의 초기화에서 init 당신의 origin 오브젝트 할 걸? – Jack

답변

2

나는

+0

좀 더 구체적으로 기재 할 수 있습니까? –

+2

@ProgrammingNerd 어떤 의미로? alloc-init을 할 것인가? 그렇지 않은 경우 그렇게하십시오. –

+0

그런 다음 첫 번째 문제는 객체에서 alloc-init을 수행하지 않으면 왜 작동합니까? –

관련 문제