Objective-C Data Encapsulation
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的更多相关文章
- Python : Data Encapsulation
Python : Data Encapsulation The following table shows the different behaviour: Name Notation Behavio ...
- [转]C语言SOCKET编程指南
1.介绍 Socket编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措?等 ...
- C++ 系列:socket 资料收集
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- C语言SOCKET编程指南
1.介绍 Socket 编程让你沮丧吗?从man pages中很难得到有用的信息吗?你想跟上时代去编Internet相关的程序,但是为你在调用 connect() 前的bind() 的结构而不知所措? ...
- 深入理解OOP(第一天):多态和继承(初期绑定和编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...
- 深入浅出OOP(一): 多态和继承(早期绑定/编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...
- CLR via C#深解笔记四 - 方法、参数、属性
实例构造器和类(引用类型) 构造器(constructor)是允许将类型的实例初始化为良好状态的一种特殊方法.构造器方法在“方法定义元数据表”中始终叫.ctor. 创建一个引用类型的实例时: #1, ...
- WireShark数据包分析数据封装
WireShark数据包分析数据封装 数据封装(Data Encapsulation)是指将协议数据单元(PDU)封装在一组协议头和尾中的过程.在OSI七层参考模型中,每层主要负责与其它机器上的对等层 ...
- 10.Properties
The common language runtime (CLR) offers two kinds of properties: 1.parameterless properties, which ...
随机推荐
- String类无子类
1. 关于final修饰符 参考文章: 浅析Java中的final关键字 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量 ...
- Tautonym Puzzle
题意: 构造一个长度不超过200,数字不大于100的序列,使得合法子序列的个数恰好为N: 合法子序列是指一个长度为偶数的序列,前一半和后一半相等. 解法: 考虑这种构造方法 假设我们当前有序列为 $x ...
- XP系统显示控件异常解决方法
XP下显示WPF控件异常,一般通过关闭Direct 3D加速即可.1.按“WIN”+R键,在“运行”输入框中输入“dxdiag”:2.在DirectX诊断工具”对话框,选择“显示”页面,在“Direc ...
- java 大数详细讲解
介绍 java中用于操作大叔的类主要有俩种 第一个是BigInteger,代表大整数.第二个是BigDecimal,代表大浮点数.两种类的操作方法类似,所以我们只讲解BigInterger的用法 基本 ...
- 基于thinkphp5的Excel上传
涉及知识点: thinkphp5.0: excel上传: mysql建立新表(基本的create语句): mysql ignore(避免重复插入): 主要功能: 通过在视图中上传excel文件,在my ...
- HDU4791【杂】
题意: 给你一个从0开始的区间si,每个区间是前闭后开,[ s[i] , s[i+1] ), 然后再给你个一个pi,代表你在区间[ s[i] , s[i+1] )里面买东西的单价是pi,给出的s1一定 ...
- CF364D Ghd(随机化)
另一个集合\(s\)的\(ghd\)为\(max\{gcd(s')||s'|>=0.5|s|\}\) 给定序列\(a\),求\(ghd\) 随机化算法.因为\(|s'|\geq 0.5|S|\) ...
- app发布证书、真机调试证书、测试证书、推送证书详细过程
原文网址: http://www.cnblogs.com/cxbblog/p/4028159.html 一:发布证书 遵旨:哪个开发者的哪台电脑要发布哪个app (这句话可以多读几遍) 通过上边的遵旨 ...
- JVM图解
站在巨人的肩膀上 https://www.jianshu.com/p/314272e6d35b 1. Minor GC (1) Minor GC过程 假设现在Heap内存大小为20M,其中年轻代为10 ...
- react native 获取地图需要的SHA1
1.从电脑的根目录进入.android文件 2.进入.android文件后输入 keytool -v -list -keystore debug.keystore 3.回车输入密码,(可以直接回车不用 ...