@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;
}
這工作如我所料不錯。但是,如果我改變了方法
setOrigin: (XYPoint *) pt
在Rectangle類,以
-(void) setOrigin: (XYPoint *) pt
{
origin.x = pt.x;
origin.y = pt.y;
}
它只是打印的0 x和y的值。這兩種方法有什麼區別。對我來說,似乎是一樣的。
你能提供'XYPoint'和'Rectangle'太'@ interface'? – Jack 2013-03-17 20:54:44