All Objective-C programs are composed of the following two fundamental elements:

  • Program statements (code): This is the part of a program that performs actions and they are called methods.

  • Program data: The data is the information of the program which is affected by the program functions.

Encapsulation is an Object-Oriented Programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

Objective-C supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. For example:

@interface Adder : NSObject
{
NSInteger total;
} - (id)initWithInitialNumber:(NSInteger)initialNumber; - (void)addNumber:(NSInteger)newNumber; - (NSInteger)getTotal; @end

The variable total is private and we cannot access from outside the class. This means that they can be accessed only by other members of the Adder class and not by any other part of your program. This is one way encapsulation is achieved.

Methods inside the interface file are accessible and are public in scope.

There are private methods, which are written with the help of extensions, which we will learn in upcoming chapters.

Data Encapsulation Example:

Any Objective-C program where you implement a class with public and private members variables is an example of data encapsulation and data abstraction. Consider the following example:

#import <Foundation/Foundation.h>

@interface Adder : NSObject
{
NSInteger total;
} - (id)initWithInitialNumber:(NSInteger)initialNumber; - (void)addNumber:(NSInteger)newNumber; - (NSInteger)getTotal; @end @implementation Adder -(id)initWithInitialNumber:(NSInteger)initialNumber{
total = initialNumber;
return self;
} - (void)addNumber:(NSInteger)newNumber{
total = total + newNumber;
} - (NSInteger)getTotal{
return total;
} @end int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Adder *adder = [[Adder alloc]initWithInitialNumber:];
[adder addNumber:];
[adder addNumber:];
NSLog(@"The total is %ld",[adder getTotal]);
[pool drain];
return ;
}

When the above code is compiled and executed, it produces the following result:

-- ::30.485 DataEncapsulation[:] The total is 

Above class adds numbers together and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world, but is needed for the class to operate properly.

Designing Strategy:

Most of us have learned through bitter experience to make class members private by default unless we really need to expose them. That's just good encapsulation.

It's important to understand data encapsulation since it's one of the core features of all Object-Oriented Programming (OOP) languages including Objective-C.

Objective-C Data Encapsulation的更多相关文章

  1. Python : Data Encapsulation

    Python : Data Encapsulation The following table shows the different behaviour: Name Notation Behavio ...

  2. [转]C语言SOCKET编程指南

    1.介绍 Socket编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等 ...

  3. C++ 系列:socket 资料收集

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  4. C语言SOCKET编程指南

    1.介绍 Socket 编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措? ...

  5. 深入理解OOP(第一天):多态和继承(初期绑定和编译时多态)

    在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...

  6. 深入浅出OOP(一): 多态和继承(早期绑定/编译时多态)

    在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...

  7. CLR via C#深解笔记四 - 方法、参数、属性

    实例构造器和类(引用类型) 构造器(constructor)是允许将类型的实例初始化为良好状态的一种特殊方法.构造器方法在“方法定义元数据表”中始终叫.ctor. 创建一个引用类型的实例时: #1, ...

  8. WireShark数据包分析数据封装

    WireShark数据包分析数据封装 数据封装(Data Encapsulation)是指将协议数据单元(PDU)封装在一组协议头和尾中的过程.在OSI七层参考模型中,每层主要负责与其它机器上的对等层 ...

  9. 10.Properties

    The common language runtime (CLR) offers two kinds of properties: 1.parameterless properties, which ...

随机推荐

  1. SQL Server 2008将数据导出为脚本 [SQL Server]

    之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...

  2. Vue 项目中添加全局过滤器以及全局混合mixin

    可以在.vue文件中定义局部使用的过滤器 export default{ data(){ return [] }, filters:{ toUpperCase:function(value){ ret ...

  3. windows: Python安装scipy,scikit-image时提示"no lapack/blas resources found"的解决方法

    解决方案: 最后,解决我遇到的这个问题的解决方案是来自以下链接的答案: http://www.voidcn.com/blog/z6491679/article/p-5740396.html. 另外还有 ...

  4. 【223】◀▶ IDL HDF 文件操作说明

    参考:I/O - HDF Routines —— HDF 操作函数 01   HDF_SD_START 打开一个 SDS 模式的 HDF 文件. 02   HDF_SD_END 关闭一个 SDS 模式 ...

  5. 使用Bootstrap模态框实现增删改查功能

    模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. 本文实现的是使用模态框实现简单的增删改查的功能. ...

  6. 微信小程序开发之实现https

       1:使用自签名的免费ssl证书实现:http://jingyan.baidu.com/article/a948d6515d3e850a2dcd2ee6.html           2:迅雷云购 ...

  7. Lightoj1018 【状压DP】

    题意: 给你一个坐标系,坐标系上有N个点,然后让你用最少的线,把这些点全部连起来: 思路: (1+15)*15/2=90条线: 然后线上有哪些点就可以知道: 然后按照线上点的个数排序,然后删掉这个线, ...

  8. LightOJ 1022 【读题】

    求阴影面积: 犯了两个错误,漏看了两个条件. 第一个wa:题面中PI说要取pi = 2 * acos (0.0) 第二个wa: For example, add 10-9 to your result ...

  9. Lightoj1011【KM算法】

    题意: 问男孩女孩最大的可能值?其实就是一个二分图的最大权值匹配问题:模板题吧.. #include<cstdio> #include<math.h> #include< ...

  10. HDU 1556【线段树区间更新】

    这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...