oc 中四种实例变量的范围类型@private@protected@public@package

时间:2022-12-04 09:39:28

To enforce the ability of an object to hide its data, the compiler limits the scope of instance variables—that is, limits their visibility within the program.

为了强制一个对象隐藏其数据,编译器限制实例变量范围以限制其在程序中的可见性

But to provide flexibility, it also lets you explicitly set the scope at four levels. Each level is marked by a compiler directive:

但是为了提供灵活性,苹果也让开发者显式设置范围(四选一)

Directive

Meaning

@private

The instance variable is accessible only within the class that declares it.

@protected

实例变量只能被声明它的类访问

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

实例变量能被声明它的类和子类访问,所有没有显式制定范围的实例变量都是@protected

@public

The instance variable is accessible everywhere.

实例变量可以被在任何地方访问。

@package

Using the modern runtime, an @package instance variable has @public scope inside the executable image that implements the class, but acts like @private outside.使用modern运行时,一个@package实例变量在实现这个类的可执行文件镜像中实际上是@public的,但是在外面就是@private【runtime需要再看一下苹果文档Runtime Programming Guide

The @package scope for Objective-C instance variables is analogous to private_extern for C variables and functions. Any code outside the class implementation’s image that tries to use the instance variable gets a link error.

Objective-C中的@package与C语言中变量和函数的private_extern类似。任何在实现类的镜像之外的代码想使用这个实例变量都会引发link error

This scope is most useful for instance variables in framework classes, where @private may be too restrictive but @protected or @public too permissive.

这个类型最常用于框架类的实例变量,使用@private太限制,使用@protected或者@public又太开放

oc 中四种实例变量的范围类型@private@protected@public@package的更多相关文章

  1. iOS中四种实例变量的范围类型@private@protected@public@package

    文档上记录是这样的 The Scope of Instance Variables Toenforce the ability of an object to hide its data, the c ...

  2. OC中四种遍历方式

    标准的C语言for循环.Objective-C 1.0出现的NSEnumerator.Objective-C 1.0出现的for in快速遍历.块遍历. 遍历的话,一般是NSArray.NSDicti ...

  3. java中四种引用类型(对象的强、软、弱和虚引用)

    对象的强.软.弱和虚引用在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2 ...

  4. Java中四种引用:强、软、弱、虚引用

    这篇文章非常棒:http://alinazh.blog.51cto.com/5459270/1276173 Java中四种引用:强.软.弱.虚引用 1.1.强引用当我们使用new 这个关键字创建对象时 ...

  5. [转]C++中四种类型转换符的总结

    C++中四种类型转换符的总结 一.reinterpret_cast用法:reinpreter_cast<type-id> (expression)    reinterpret_cast操 ...

  6. OC中两种单例实现方式

    OC中两种单例实现方式 写在前面 前两天探索了一下C++ 的单例,领悟深刻了许多.今天来看看OC中的单例又是怎么回事.查看相关资料,发现在OC中一般有两种实现单例的方式,一种方式是跟C++ 中类似的常 ...

  7. java中四种引用类型

    java中四种引用类型  今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...

  8. C&num;中四种常用集合的运用&lpar;非常重要&rpar;

    C#中4个常用的集合 1.ArrayList ArrayList类似于数组,有人也称它为数组列表.ArrayList可以动态维护,而数组的容量是固定的. 它的索引会根据程序的扩展而重新进行分配和调整. ...

  9. C&plus;&plus;中四种类型转换以及const&lowbar;cast是否能改变常量的问题

    we have four specific casting operators:dynamic_cast, reinterpret_cast, static_cast and const_cast. ...

随机推荐

  1. codevs 1115 开心的金明--01背包

    1115 开心的金明 2006年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 金明今天很开心,家里购 ...

  2. Java-单机版的书店管理系统&lpar;练习设计模块和思想&lowbar;系列汇总&rpar;

    介绍: 本软件系列到此,我已经全部写完了. 项目练习目标 : 1.Java应用程序基本分析 2.培养面向对象编程的基本思想 3.Java基本设计模式综合应用 4.掌握分层和接口的基本设计 5.构建合理 ...

  3. Emgu学习笔记(一)安装及运行Sample

    1.简单说明 Emgu是Dot Net平台对OpenCV的封装,本质上没有增加新功能,是通过Dot Net的平台调用技术直接调用OpenCV C++语言写的库,使用我们可以方便用.net平台通过Ope ...

  4. FusionCharts使用问题及解决方法&lpar;一&rpar;-FusionCharts常见问题大全

    当你利用FusionCharts创建图表时报错,原因是多方面的.本文总结了一些常见问题及解决方法供大家参考,希望对大家有帮助. 如果你无法看到任何图表: 如果浏览器中的进度条一直加载,或者右键菜单显示 ...

  5. &lbrack;iOS&rsqb;C语言技术视频-16-指针变量高级用法&lpar;堆栈内存&rpar;

    下载地址: 链接: http://pan.baidu.com/s/1qWqWnGo 密码: igjc

  6. jquery ajax promise

    $request = $.getJSON('test.php'); $request.done(process1); $request.done(process2); $request.always( ...

  7. nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket【转】

    原文地址:https://www.cnxct.com/default-configuration-and-performance-of-nginx-phpfpm-and-tcp-socket-or-u ...

  8. 3、zabbix配置入门

    Zabbix模板 zabbix组件:    zabbix-server    zabbix-database    zabbix-web    zabbix-agent    zabbix-proxy ...

  9. &lbrack;转&rsqb; javascript另类方法高效实现htmlencode&lpar;&rpar;与htmldecode&lpar;&rpar;函数

    本文转自:http://blog.csdn.net/cuixiping/article/details/7846806 最常见的做法是采用正则表达式替换的方法,将特殊字符如 < > &am ...

  10. bzoj 3796 Mushroom追妹纸 —— 后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3796 先把三个串拼在一起,KMP 求 s1 , s2 中每个位置和 s3 的匹配情况: 注意 ...