我試圖通過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感謝爲您的快速回復