@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의 값을 출력한다. 이 두 가지 방법의 차이점은 무엇입니까? 나를 위해, 그것은 같아 보인다.
당신이 너무'XYPoint'와'Rectangle'의'@의 interface'를 제공 할 수 있습니다 ... 당신이하지
alloc
과Rectangle
클래스의 초기화에서init
당신의origin
오브젝트 할 걸? – Jack