11

私は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;私はそれのためのセッターを作成しませんでした。

ところで、あなたの速い返事に感謝します

4

5 に答える 5

24

私が理解していないのは、メインのこの行です:myRect.origin = myPoint;私はそれのためのセッターを作成しませんでした。

クラスに、ゲッターとセッター(まとめてアクセサーと呼ばれる)の両方が作成されoriginていRectangleます。の実装をご覧になるとRectangle、これがゲッターです。

-(XYPoint *) origin
{
    return origin;
}

そしてこれはセッターです:

- (void) setOrigin: (XYPoint *) pt
{
    origin = pt;
}

そしてObjective-C2.0の呼び出しの時点で:

myRect.origin = myPoint;

と同等です:

[myRect setOrigin:myPoint];

を使用してゲッターとセッターを宣言する@property(そしてを使用してそれらを実装する@synthesize)ことは、アクセサーを宣言および作成する1つの方法にすぎず、クラスインターフェイスで宣言するプロパティがたくさんある場合に便利です。前述Schildmeijerのように、@property int width2つのメソッドを宣言することと同じです。

- (int)width;
- (void)setWidth:(int)newWidth;

Objective-Cメソッド呼び出しは動的にバインドされるため、インターフェイスでgetterメソッドとsetterメソッドを宣言する必要はありませんが、他のユーザーが公開しているものとして宣言する場合は、一般的に宣言するのがベストプラクティスです。クラス。

于 2010-04-11T11:25:06.957 に答える
7

プロパティ宣言は、2つのアクセサメソッドを宣言することと同等であると考えることができます。したがって

@property int width;

と同等です:

- (int)width;

- (void)setWidth:(int)newWidth;
于 2010-04-11T09:41:02.623 に答える
2
//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property int Width;
@property int Height;
-(int)Area;
@end

//Rectangle.m
#import "Rectangle.h"
@implementation Rectangle
@synthesize Width;/*Will create value Width , Setter called"setWidth" and Getter called "Width"*/
@synthesize Height;/*Will create value Height , Setter called"setHeight" and Getter called "Height"*/

-(int)Area
{
    return Width*Height;
}
@end


//  main.m
#import <Cocoa/Cocoa.h>
#import "Rectangle.h"
int main(int argc, const char * argv[])
{
    Rectangle *myRectangle = [Rectangle new];

    myRectangle.Width=3;
    myRectangle.Height=5;
    printf("Area = %d\n",[myRectangle Area]);

    //Or
    [myRectangle setWidth:5];
    [myRectangle setHeight:6];
    printf("Area = %d\n",[myRectangle Area]);

}

Getterのみを作成する場合、またはgetterとsetterの名前を変更する場合

•読み取り専用

•getter=newGetterName

•setter=new SetterName

//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (getter = getWidth) int Width;
@property (readonly) int Height;
@end
于 2015-03-13T12:00:19.010 に答える
0

どのコードが機能しているのか、または「機能している」ことに対する期待は何であるかはわかりません。

上記のインターフェースは、他のオブジェクトから呼び出すことができる幅と高さの単純なアクセサーメソッドを作成します。これら2[object setWidth:1];object.width = 1;は類似しています。

Originは他のオブジェクトタイプであり、ポインタです。ただし、アクセサメソッドを生成するためにプロパティを宣言する必要があります。

于 2010-04-11T09:42:07.577 に答える
0

ゲッターとセッターは、別のクラスからインスタンス変数にアクセスする必要がある場合、またはバインディングを使用してそれらを取得/設定している場合に最も役立ちます。したがって、幅と高さにはこの機能が必要ですが、原点には必要ないと思います。あなたが述べたように、ゲッター/セッターは整数からポインターを作成しないことに注意してください。Intsはintであり、getter/setterはそれを変更しません。

于 2010-04-11T09:45:48.757 に答える