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 ...
随机推荐
- 算法实现c语言--01
打印九九乘法表 #include<stdio.h> #include<stdlib.h> int main() { , j = ; ; i <= ; i++) { ; j ...
- CSS:CSS 选择器参考手册
ylbtech-CSS:CSS 选择器参考手册 1.返回顶部 1. 我们会定期对 W3School 的 CSS 参考手册进行浏览器测试. CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需 ...
- 转:ubuntu下安装ssh服务
本文内容来自 http://www.cnblogs.com/chen1987lei/archive/2010/12/02/1894768.html ========================= ...
- 创建calico网络报错client response is invalid json
使用docker创建calico网络失败. # docker network create --driver calico --ipam-driver calico-ipam testcalico E ...
- 性能测试之Jmeter学习(四)
本节主要讲解:如何创建Web测试计划 如何创建一个简单的测试计划,用于测试web站点? 1.明确测试需求:我们会模拟5个并发用户,对Jakarta Web站点的网个页面进行访问,另外每个并发用户都会运 ...
- 2.6 hive分区表
一.背景 ######### 分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件. Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成更小的数据集. ...
- 1 python----pycharm本地部署spark
下图相关工具连接 链接:https://pan.baidu.com/s/115XWf_Fc1yMiJytKJQXnFQ 密码:3jvr 好了,加油哟!
- ubuntu的 mysql 存储目录迁移
1:sudo service MySQL stop#迁移前必须先停止mysql 2:创建mysql 存放的 目标文件夹 一般 默认的 mysql 存储目录在 /var/lib中 看清楚 文件的权限 ...
- roguelike地牢生成算法
文章原地址 上一个地图生成算法,这一次是一个地牢的生成算法,是一个国外的人写的算法,用dart语言写,我把它改成了unity-c#. 原作者博客地址:Rooms and Mazes: A Proced ...
- c# 调用 webservices (转载)
.net 调用webservice 总结 最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice . 我们都知道,调用webserivice 最简单 ...