Objective-C获取器/设置器 [英] Objective-C getter/ setter

查看:64
本文介绍了Objective-C获取器/设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Objective-C教程进行工作.书中有这个例子:

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;

我想,嘿,XYPoint对象没有getter/setter方法.但是代码确实起作用了."现在我可能要回答我自己的问题了:).

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..

我是对的,还是我在说BS :) ??

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

这是Rectangle对象:

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

我不明白的是这行主要内容:myRect.origin = myPoint;我没有为此设置传承人..

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

推荐答案

我不明白这是主行:myRect.origin = myPoint;我没有为此设置传承人..

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

Rectangle类中为origin创建的既是getter又是setter(统称为 accessors ).如果您看一下Rectangle的实现,那就是吸气剂:

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

这是二传手:

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

从Objective-C 2.0调用开始:

And as of Objective-C 2.0 calling:

myRect.origin = myPoint;

等效于:

[myRect setOrigin:myPoint];

使用@property声明getter和setter(然后使用@synthesize实施它们)只是声明和创建访问器的一种方法,如果您在类接口中有很多要声明的属性,可以方便使用.正如Schildmeijer所说,@property int width等同于声明两种方法:

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;

由于Objective-C方法调用具有动态绑定性质,您甚至没有必须在接口中声明getter和setter方法,尽管这样做通常是最佳做法如果您要宣传这些广告,以供其他班级公开使用.

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.

这篇关于Objective-C获取器/设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆