swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明
关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明
open,public,fileprivate,private,internal 这几个修饰词的作用是用于修饰访问级别的。
open,public 对应的级别是该模块或者是引用了该模块的模块可以访问 即 a belong to A , B import A 这两种情况都可以对 a进行访问
public: 类用public(或级别更加等级更低的约束(如private等))修饰后只能在本模块(sdk)中被继承,如果public是修饰属性的话也是只能够被这个module(sdk)中的子类重写
open:用open修饰的类可以在本某块(sdk),或者其他引入本模块的(sdk,module)继承,如果是修饰属性的话可以被此模块或引入了此某块(sdk)的模块(sdk)所重写
open 和public 如果是用于继承某个类的话,那么open 和 public 必须是在父类的约束限定之下, 如:不能父类是用public修饰的子类却用open修饰。
、internal 是在模块内部可以访问,在模块外部不可以访问,a belong A , B import A, A 可以访问 a, B 不可以访问a.比如你写了一个sdk。那么这个sdk中有些东西你是不希望外界去访问他,这时候你就需要internal这个关键字(我在导入第三方框架时发现其实没有定义的话sdk里面是默认internal的)
、fileprivate 这个修饰跟名字的含义很像,file private 就是文件之间是private的关系,也就是在同一个source文件中还是可以访问的,但是在其他文件中就不可以访问了 a belong to file A, a not belong to file B , 在 file A 中 可以访问 a,在 file B不可以访问a
、private 这个修饰约束性比fileprivate的约束性更大,private 作用于某个类,也就是说,对于 class A ,如果属性a是private的,那么除了A外其他地方都不能访问了(fileprivate 和private都是一种对某个类的限制性约束。fileprivate的适用场景可以是某个文件下的extension(由于Extention不能存储property所以,这里的fileprivate是针对func有效的),如果你的类中的变量定义成了private那么这个变量在你这个类在这个类的文件的拓展中就无法访问了,这时就需要定义为fileprivate)
最后是 Guiding Principle of Access Levels (访问级别的推导原则):不能在低级别的修饰中定义比自身更高的级别修饰,如public不能修饰在private类中的属性
首先说明一下这里面的区别在文章结尾英文或链接中可以看得到
Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.
Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.
Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
Private access restricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.
Open access applies only to classes and class members, and it differs from public access as follows:(public 和 open 的区别)
Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.
Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.
Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.
Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.
Marking a class as open explicitly indicates that you’ve considered the impact of code from other modules using that class as a superclass, and that you’ve designed your class’s code accordingly.
Guiding Principle of Access Levels
Access levels in Swift follow an overall guiding principle: No entity can be defined in terms of another entity that has a lower (more restrictive) access level.
For example:
A public variable cannot be defined as having an internal, file-private, or private type, because the type might not be available everywhere that the public variable is used.
A function cannot have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are not available to the surrounding code.
The specific implications of this guiding principle for different aspects of the language are covered in detail below.
swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明的更多相关文章
- java中的public,protected,private权限修饰
public和private基本没问题,主要是默认的和protected之间的区别 同一包中默认的和protected一样,所以来看看不同包的情况 看下如下代码,两个类位于不同包: public cl ...
- public, protected, private,internal,protected internal的区别
虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal ...
- Ruby中访问控制符public,private,protected区别总结
重点关注private与protected public 默认即为public,全局都可以访问,这个不解释 private C++, “private” 意为 “private to this cla ...
- Java public 和 private 访问修饰符
何为封装 从事面向对象编程的 Java 程序员,不可能不知道封装,它是面向对象编程的精髓,非常重要. 那什么是封装?字面意思就是把摆在外面的东西包起来. 一句话,封装就是对外隐藏内部细节. 那为何要封 ...
- c# 修饰词public, protected, private,internal,protected的区别
public: 对任何类和成员都公开, 无限制访问;protected: 仅仅对该类以及该类的派生类公开;private: 仅仅对该类公开;internal: 只能值包含该类的程序集中访问该类(只是 ...
- public, protected, private, internal, protected internal简析
public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...
- 访问控制中默认,public,private,protected区别?
2.继承的访问控制: (比如一个类中的protected成员对于"不同的包中的非子类"是不可见的. 说明:1.任何public的内容可以被从任何地方访问. 2.private的成员 ...
- c++,public/protected/private权限修饰符
1.public的变量可以在类中以及外部访问到: 2. private只可以在类/友元中访问到. #include <iostream> using namespace std; //-- ...
- 谈Swift中的访问控制
访问控制(Access Control) 访问控制可以限定其他源文件或模块中的代码对你的代码的访问级别.这个特性可以让我们隐藏代码的一些实现细节,并且可以指定一些代码和访问和使用的优先接口. 你可以明 ...
随机推荐
- 先说IEnumerable,我们每天用的foreach你真的懂它吗?
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 实现代理设置proxy
用户在哪些情况下是需要设置网络代理呢? 1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网:多个电脑共享上外网,就要用代理: 2.有些网页被封,通过国外的代理就能看到这被封的网站:3. ...
- inline-block元素间距问题的几种解决方案
不知道大家有没有碰到过设置了display:inline-block;的几个相邻元素之间有几px间距的问题,这里提供几种简单实用的解决方法,希望能够帮到大家! 方法1. 将<li>标签 ...
- Java 为值传递而不是引用传递
——reference Java is Pass by Value and Not Pass by Reference 其实这个问题是一个非常初级的问题,相关的概念初学者早已掌握,但是时间长了还是容易 ...
- Axure 8.0.0.3312可用注册码
用户名:aaa 注册码:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajxGU 用户名:axureuser 序列号:8wFfI ...
- BPM配置故事之案例11-操作外部数据源
小明:可以获取ERP数据了-- 老李:哦,这么快?小伙子,我非常看好你,来来,别急着走,再陪我聊会-- 小明:--您老人家不是又要改流程吧? 老李:没有没有,哎嘿嘿嘿,我们这不都是为公司效率着想嘛,这 ...
- 一点公益商城开发系统模式Ring Buffer+
一个队列如果只生产不消费肯定不行的,那么如何及时消费Ring Buffer的数据呢?简单的方案就是当Ring Buffer"写满"的时候一次性将数据"消费"掉. ...
- maven常见异常以及解决方法
本文写的是我在整合ssm框架时遇到的一些问题,我估计很多人也会遇到,所以,这里作为一个总结,希望能够帮助大家解决问题 一,加入shiro组件时抛出的异常 加入步骤(略) 问题 1,保存后,无法导入sh ...
- 绿色版的Linux.NET——“Jws.Mono”(续)
在前一篇文章中,我们简略的介绍了jws.mono的安装使用,以及我们如何自己动手做一个jws.mono出来. 在文章发表之后的几天里,我一直觉得有点不妥之处,直到后来猛然的意识到:我们自己动手做的jw ...
- TDD原则
TDD 介绍 测试驱动开发,或者叫 TDD,是一个敏捷方法,通过确保在代码是先前手动编写测试用 例,用测试来驱动开发,从而翻转开发生命周期(它不只是作为一种校验工具). TDD 的原则很简单的: 只有 ...