Objective-C(iPhone 开发)

发布于 2024-12-03 19:18:57 字数 358 浏览 1 评论 0原文

Possible Duplicate:
Explain to me what is a setter and getter

What are setters and getters? couldn't find it on wikipedia and in other places.

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

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

发布评论

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

评论(3

一身软味 2024-12-10 19:18:58

Getters 和 Setters 是对象上的方法,允许您隐藏某些内容的内部实现,但仍然允许其他对象访问某些内容。它们还允许您在调用它们时执行验证或其他任务。

在 Objective-C 中,它们与属性的概念联系在一起——如果你定义一个属性,你就会得到对象函数的 getter 和 setter。

例如,如果您想知道某件事被请求了多少次怎么办?

@interface MyDataHolder {
    NSData *data;
    NSInteger count;
}

@property (readonly) NSData *data;

@end

现在,在您的 .m 文件中

@implementation MyDataHolder
...
- (NSData *)data {
   count ++;
   return data;
}

,只需请求数据,计数也会增加。

这里有更多关于属性的文档这里和@jussi 的链接是对 getter/setter 的更好概述。

Getters and Setters are methods on an object that allow you to hide the internal implementation of something but still allow other objects to access things. They also allow you to do validation or other tasks when they are called.

In objective-c, they are tied to the idea of properties - if you define a property you get getters and setters for the object's function.

For example, what if you wanted to know how many times something has been asked for?

@interface MyDataHolder {
    NSData *data;
    NSInteger count;
}

@property (readonly) NSData *data;

@end

and in your .m file

@implementation MyDataHolder
...
- (NSData *)data {
   count ++;
   return data;
}

Now, just by asking for the data, count is incremented as well.

There's more documentation about properties here and @jussi's link is a better overview of getters/setters in general.

旧夏天 2024-12-10 19:18:58

它们通常被称为变异器访问器

搜索 mutator/accessor 产生以下结果:

在计算机科学中,增变方法是一种用于控制变量更改的方法。
mutator 方法,有时称为“setter”,最常用于面向对象编程,以符合封装原则。根据这一原则,类的成员变量被设为私有,以隐藏并保护它们免受其他代码的影响,并且只能由公共成员函数(mutator 方法)修改,该函数将所需的新值作为参数,可选地验证它,并修改私有成员变量。
通常,“setter”伴随有“getter”(也称为访问器),它返回私有成员变量的值。

http://en.wikipedia.org/wiki/Mutator_method


您可以在 Onbjectice 中使用此范例-C 通过 声明的属性

@interface MyClass : NSObject
{
    NSString *value;
}
@property(copy, readwrite) NSString *value;
@end

@implementation MyClass
@synthesize value;
@end

They are ofter referred to as mutators and accessors.

A search for mutator/accessor yielded following result:

In computer science, a mutator method is a method used to control changes to a variable.
The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.
Often a "setter" is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable.

http://en.wikipedia.org/wiki/Mutator_method


You can use this paradigm in in Onbjectice-C via declared properties:

@interface MyClass : NSObject
{
    NSString *value;
}
@property(copy, readwrite) NSString *value;
@end

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