私はObjective-Cチュートリアルを進めようとしています。この本には次の例があります。
@interface
{
int width;
int height;
XYPoint *origin;
}
@property int width, height;
「XYPointオブジェクトのゲッター/セッターはありません。コードは機能しますが」と思いました。今、私は多分私自身の質問に答えるつもりです:)。
「原点」はすでにポインタであり、「幅」と「高さ」の内部で起こっていることは、それらへのポインタが作成されることになるからだと思います。
私は正しいですか、それとも私は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;
}
そして、これがRectangleオブジェクトです。
#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;
私はそれのためのセッターを作成しませんでした。
ところで、あなたの速い返事に感謝します