Objective-C getter/ setter

12

我正在学习一个Objective-C教程。书中有这个例子:

@interface
{
 int width;
 int height;
 XYPoint *origin;
}
@property int width, height;

我认为:"嘿,XYPoint对象没有getter/setter。不过代码确实可以运行。" 现在我可能会回答自己的问题 :)

我想这是因为 "origin" 已经是一个指针,而 "width" 和 "height" 在幕后发生的事情是,将会创建指向它们的指针。

我是对的还是在胡说八道 :) ??

我就是不明白。这是主函数:

#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

我不理解 main 函数中的这行代码:myRect.origin = myPoint; 我没有为它编写设置器(setter)。

顺便说一下,感谢您的快速回复。

5个回答

25

我不理解main函数中这行代码的含义:myRect.origin = myPoint; 我没有为它编写setter方法..

Rectangle 类中已经创建了一个同时包括getter和setter(统称为访问器)的 origin。如果你查看 Rectangle 的实现,你会发现这是getter:

-(XYPoint *) origin
{
    return origin;
}

这是setter:

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

截至Objective-C 2.0的现在,调用方式如下:

myRect.origin = myPoint;

等同于:

[myRect setOrigin:myPoint];

使用 @property 声明 getter 和 setter (然后使用 @synthesize 实现它们)只是声明和创建访问器的一种方式,如果您需要在类接口中声明许多属性,则这是一种方便的方法。就像 Schildmeijer 所说的,@property int width 相当于声明了两个方法:

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

由于 Objective-C 方法调用是动态绑定的,即使您不在接口中声明 getter 和 setter 方法,也可以进行调用,但如果您要将它们公开可用于其他类,则通常最佳实践是在接口中声明它们。


7
您可以将属性声明视为声明两个访问器方法的等效方式。因此,
@property int width;

等同于:

- (int)width;

- (void)setWidth:(int)newWidth;

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,请按以下方式操作:
• readonly
• getter = 新 Getter 名称
• setter = 新 Setter 名称
示例:
//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (getter = getWidth) int Width;
@property (readonly) int Height;
@end

0

你没有说明哪些代码是有效的,或者对于“有效”的期望是什么。

上述接口将创建简单的访问器方法,用于宽度和高度,可以从其他对象中调用,如[object setWidth:1];object.width = 1; - 这两个是类似的。

Origin是另一种对象类型并且是一个指针,是的。但您仍然希望为其声明一个属性以生成访问器方法。


0

如果你需要从另一个类中访问实例变量,或者使用绑定来获取/设置它们,那么Getter和Setter就非常有用。因此,我的猜测是你需要这种功能来获取和设置宽度和高度,但不是原点。请注意,Getter和Setter并没有像你所说的那样将整数变成指针。整数就是整数,Getter和Setter并不改变它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接