Objective-C getter/setter

发布于 2024-08-28 14:25:24 字数 1560 浏览 10 评论 0原文

我正在努力完成 Objective-C 教程。书中有这样一个例子:

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

我想,“嘿,XYPoint 对象没有 getter/setter。不过代码确实可以工作。”现在我也许要回答我自己的问题了:)。

我认为这是因为“原点”已经是一个指针,而“宽度”和“高度”的幕后发生的事情是,将会创建一个指向它们的指针..

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

我只是不明白。这是主要的:

#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;我没有为它制作设置器..

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

I'm trying to work my way through an Objective-C tutorial. In the book there is this example:

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

I thought, "hey there's no getter/setter for the XYPoint object. The code does work though." Now i'm going maybe to answer my own question :).

I thinks its because "origin" is a pointer already, and whats happening under the hood with "width" and "height", is that there is going te be created a pointer to them..

Am i right, or am i talking BS :) ??

I just dont get it. here's main:

#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;
}

And here's the Rectangle object:

#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

What i dont understand is this line in main: myRect.origin = myPoint; I did not make a setter for it..

BTW thanks for your fast reply's

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

未蓝澄海的烟 2024-09-04 14:25:24

我不明白的是 main 中的这一行: myRect.origin = myPoint; 我没有为它制作 setter..

。称为访问器),为 Rectangle 类中的 origin 创建。如果您查看 Rectangle 的实现,就会发现这是 getter:

-(XYPoint *) origin
{
    return origin;
}

这是 setter:

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

从 Objective-C 2.0 开始调用:

myRect.origin = myPoint;

相当于:

[myRect setOrigin:myPoint];

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

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

由于 Objective-C 方法调用的动态绑定特性,您甚至不需要 必须在接口中声明 getter 和 setter 方法,尽管如果您将它们宣传为对其他类公开可用,那么通常最好这样做。

What i dont understand is this line in main: myRect.origin = myPoint; I did not make a setter for it..

There is both a getter and a setter (collectively referred to as accessors) created for origin in the Rectangle class. If you have a look in the implementation for Rectangle, this is the getter:

-(XYPoint *) origin
{
    return origin;
}

and this is the setter:

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

And as of Objective-C 2.0 calling:

myRect.origin = myPoint;

is equivalent to:

[myRect setOrigin:myPoint];

Declaring getters and setters using @property (and then implementing them using @synthesize) is only one way of declaring and creating accessors, and is there for a convenience if you have lots of properties to declare in the class interface. As Schildmeijer said, @property int width is equivalent to declaring two methods:

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

Due to the dynamically-bound nature of Objective-C method calls, you don't even have to declare the getter and setter methods in the interface, although it is generally best practice to do so if you are advertising them as publicly available to other classes.

暖阳 2024-09-04 14:25:24

您可以将属性声明视为等效于声明两个访问器方法。因此

@property int width;

相当于:

- (int)width;

- (void)setWidth:(int)newWidth;

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

@property int width;

is equivalent to:

- (int)width;

- (void)setWidth:(int)newWidth;
谁的新欢旧爱 2024-09-04 14:25:24
//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 = newGetterName

• setter = new SetterName

示例

//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (getter = getWidth) int Width;
@property (readonly) int Height;
@end
//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]);

}

If you want to make Getter only or rename getter and setter

• readonly

• getter = newGetterName

• setter = new SetterName

example

//Rectangle.h
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
@property (getter = getWidth) int Width;
@property (readonly) int Height;
@end
无力看清 2024-09-04 14:25:24

您没有说明哪些代码正在工作,或者您对“工作”的期望是什么。

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

Origin 是其他一些对象类型,并且是一个指针,是的。但您仍然需要为其声明一个属性来生成访问器方法。

You don't say what code is working, or what your expectations are for "working".

The above interface will create simple accessor methods for width and height that can be called from other objects as [object setWidth:1]; or object.width = 1; - these two are analogous.

Origin is some other object type and is a pointer, yes. But you would still want to declare a property for it to generate accessor methods.

守望孤独 2024-09-04 14:25:24

如果您需要从另一个类访问实例变量或者您使用绑定来获取/设置它们,那么 getter 和 setter 非常有用。所以我的猜测是,您需要此功能来获取宽度和高度,但不需要原点。请注意,getters/setters 不会从整数中发出指针,正如您所说的可能是原因。整数就是整数,getter/setter 不会改变这一点。

Getters and setters are mostly useful if you need to access an instance variable from another class or you're using bindings to get/set them. So my guess would be that you need this functionality for the width and height but not for the origin. Note that the getters/setters do not make pointers out of the integers as you stated might be the reason. Ints are ints and getters/setters do not change that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文