我试图通过Objective-C教程的方式工作。在这本书中有这样的例子:Objective-C getter/setter
@interface
{
int width;
int height;
XYPoint *origin;
}
@property int width, height;
我想,“嘿有针对的Xypoint对象中没有的getter/setter的代码不工作,虽然”现在我要去回答我自己的问题:)。
我认为它是因为“出身”是一个指针已经和什么有“宽度”和“高度”引擎盖下发生,是有打算TE创建一个指向他们..
上午我的权利,还是我说BS :) ??
我只是不知道它。这里的主:
#import "Rectangle.h"
#import "XYPoint.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRect = [[Rectangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];
[myPoint setX: 100 andY: 200];
[myRect setWidth: 5 andHeight: 8];
myRect.origin = myPoint;
NSLog (@"Rectangle w = %i, h = %i",
myRect.width, myRect.height);
NSLog (@"Origin at (%i, %i)",
myRect.origin.x, myRect.origin.y);
NSLog (@"Area = %i, Perimeter = %i",
[myRect area], [myRect perimeter]);
[myRect release];
[myPoint release];
[pool drain];
return 0;
}
而这里的矩形对象:
#import "Rectangle.h"
#import "XYPoint.h"
@implementation Rectangle
@synthesize width, height;
-(void) setWidth: (int) w andHeight: (int) h
{
width = w;
height = h;
}
- (void) setOrigin: (XYPoint *) pt
{
origin = pt;
}
-(int) area
{
return width * height;
}
-(int) perimeter
{
return (width + height) * 2;
}
-(XYPoint *) origin
{
return origin;
}
@end
什么我不明白的是这一行主营:myRect.origin = myPoint;
我并没有为它制定者..
BTW感谢为您的快速回复