gpt4 book ai didi

objective-c - 数据封装...?

转载 作者:太空狗 更新时间:2023-10-30 03:37:26 26 4
gpt4 key购买 nike

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

向我解释,就好像我是 5 岁,然后就好像我是 25 岁....

谢谢你的时间,~丹尼尔

最佳答案

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

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

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

关于objective-c - 数据封装...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4004768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com