数据封装...?

发布于 2024-09-28 17:31:53 字数 137 浏览 3 评论 0原文

谁能向我解释一下 Objective-C 中的数据封装是什么?有人告诉我这是 Objective-C 的一个重要概念,但我不明白为什么...

向我解释一下,就像我 5 岁,然后就像我 25 岁一样...

感谢您的时间, ~丹尼尔

Would anyone be able to explain to me what data encapsulation in Objective-C is? I've been told that this an important concept of Objective-C but I don't see why...

Explain it to me as if I was 5 and then as if I was 25....

Thanks for your time,
~Daniel

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

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

发布评论

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

评论(2

仅冇旳回忆 2024-10-05 17:31:53

来自 http://mobile.tutsplus.com/tutorials/iphone/learn -objective-c-2/

我们所说的数据封装是指
包含该数据(可以这么说)
通过方法意味着我们可以访问它
需要使用方法。你们中的一些人
用其他语言编程并且
没听说过数据封装
可能想知道为什么我们这样做
方式。答案是,通过
封装数据,有一个很好的
开发商之间的缓冲
类和类的用户。因为
类方法管理和维护
类中的属性,它们
可以更轻松地维护数据
正直。另一个主要好处是
当开发者分发他的
类,使用它的人没有
担心内部结构
根本没有课。开发人员可能会更新
使其更快或更快的方法
高效,但这次更新是
对类的用户透明
因为他/她仍然使用同样的方法
无需更改他/她的代码。

简而言之,向用户提供开发人员希望他们拥有的东西,并“保护”其他一切。开发人员可以更改任何内部内容,而无需用户重写其代码。

如果开发人员不遵守数据封装,那么每次发布新版本的库、代码片段或整个程序时,我们都需要重写代码。

From http://mobile.tutsplus.com/tutorials/iphone/learn-objective-c-2/ :

What we mean by data encapsulation is
that data is contained (so to speak)
by methods meaning to access it we
need to use methods. Some of you who
have programmed in other languages and
havenʼt heard of data encapsulation
may be wondering why we do things this
way. The answer is that by
encapsulating data, there is a nice
cushion between the developer of a
class and the user of a class. Because
the class methods manage and maintains
the attributes within the class, they
can more easily maintain data
integrity. Another major benefit is
that when a developer distributes his
class, the people using it donʼt have
to worry about the internals of the
class at all. A developer may update a
method to make it faster or more
efficient, but this update is
transparent to the user of the class
as he/she still uses the same method
with no change to his/her code.

In simple terms, the user is provided with what the developer wanted them to have, and "protects" everything else. The developer can change anything internal without the user having to rewrite their code.

If developers did not conform to data encapsulation, we would need to rewrite our code every time a new version of a library, code snippet, or an entire program was released.

℉絮湮 2024-10-05 17:31:53

Objective-C 中的数据封装意味着只有类本身才能接触它的实例变量。因此,您应该始终将它们标记为私有,并且仅通过属性公开它们,如下所示:

@interface Foo : NSObject {
@private
    int numberOfBars;
    Baz* contentBaz;
}
@property(nonatamic, assign) int numberOfBars;
@property(nonatomic, retain) Baz* contentBaz;
@end

这意味着该类可以在其 setter 方法中实现验证。如果您使用 @synthesize 来生成 getter 和 setter,那就更好了,您根本不需要担心 Cocoa 的内存模型(除了在 dealloc 中释放 ivars 之外)。

Data encapsulation in Objective-C means that only the class itself should touch it's instance variables. Therefor you should always mark them as private, and only expose them through properties, as this:

@interface Foo : NSObject {
@private
    int numberOfBars;
    Baz* contentBaz;
}
@property(nonatamic, assign) int numberOfBars;
@property(nonatomic, retain) Baz* contentBaz;
@end

This means that the class can implement validation in it's setter methods. And even better if you use @synthesizeto generate your getter and setters than you need not even worry about the memory model of Cocoa at all (with the exception of releasing your ivars in dealloc).

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