所以这是我正在学习的书中的一个练习。我的练习工作正常,我理解了大部分内容,除了,而且,我不知道我是否在这里完全遗漏了一些东西,但是......编译器到底是怎么知道的:
x = 168.354 和 y = 987.259 属于 (XYPoint ) pt*
然后
x = 10.00 和 y = 10.00 属于 (XYPoint ) t*
我了解 myPoint 和 myTranslate 如何是 XYPoint 类中的单独对象以及诸如此类的东西,并且它们指向存储 X 和 Y 的内存引用,但是它如何将上述值分配给pt和t。我在这里错过了什么巨大的东西吗?
提前致谢。
这是代码:
界面
#import "XYPoint.h"
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property float width, height;
-(XYPoint*) origin;
-(void) translate: (XYPoint *)t;
-(void) setOrigin: (XYPoint *) pt;
-(float) area;
-(float) perimiter;
-(void) setHeight:(float) h andWidth: (float) w;
@end
Implementation
#import "Rectangle.h"
#import "XYPoint.h"
@implementation Rectangle
{
XYPoint *origin ;
}
@synthesize height, width;
-(void) setHeight: (float) h andWidth: (float) w;
{
width = w;
height = h;
}
-(void) setOrigin:(XYPoint *)pt
{
if (! origin)
origin = [[XYPoint alloc]init];
origin.x = pt.x;
origin.y = pt.y;
}
-(void) translate: (XYPoint*)t
{
origin.x = origin.x + t.x;
origin.y = origin.y + t.y;
}
-(float) area
{
return width * height;
}
-(float) perimiter
{
return (width + height) * 2;
}
-(XYPoint *) origin
{
return origin;
}
@end
主要的
#import "XYPoint.h"
#import "rectangle.h"
#import "Square.h"
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *myRect = [[Rectangle alloc]init];
XYPoint *myPoint = [[XYPoint alloc]init];
XYPoint *myTranslate = [[XYPoint alloc] init];
[myPoint setX: 168.354 andY: 987.259];
[myTranslate setX: 10.00 andY: 10.00 ];
myRect.origin = myPoint;
NSLog (@"Origin at %f, %f", myRect.origin.x, myRect.origin.y);
[myRect translate: myTranslate];
NSLog (@"Origin at %f, %f", myRect.origin.x, myRect.origin.y);
}
return 0;
}
输出:
起源于 168.354004、987.258972 转换为 178.354004、997.258972