关于 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类中的属性

首先说明一下这里面的区别在文章结尾英文或链接中可以看得到

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3

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,修饰的说明的更多相关文章

  1. java中的public,protected,private权限修饰

    public和private基本没问题,主要是默认的和protected之间的区别 同一包中默认的和protected一样,所以来看看不同包的情况 看下如下代码,两个类位于不同包: public cl ...

  2. public, protected, private,internal,protected internal的区别

    虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal ...

  3. Ruby中访问控制符public,private,protected区别总结

    重点关注private与protected public 默认即为public,全局都可以访问,这个不解释 private C++, “private” 意为 “private to this cla ...

  4. Java public 和 private 访问修饰符

    何为封装 从事面向对象编程的 Java 程序员,不可能不知道封装,它是面向对象编程的精髓,非常重要. 那什么是封装?字面意思就是把摆在外面的东西包起来. 一句话,封装就是对外隐藏内部细节. 那为何要封 ...

  5. c# 修饰词public, protected, private,internal,protected的区别

    public:  对任何类和成员都公开, 无限制访问;protected: 仅仅对该类以及该类的派生类公开;private: 仅仅对该类公开;internal: 只能值包含该类的程序集中访问该类(只是 ...

  6. public, protected, private, internal, protected internal简析

    public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...

  7. 访问控制中默认,public,private,protected区别?

    2.继承的访问控制: (比如一个类中的protected成员对于"不同的包中的非子类"是不可见的. 说明:1.任何public的内容可以被从任何地方访问. 2.private的成员 ...

  8. c++,public/protected/private权限修饰符

    1.public的变量可以在类中以及外部访问到: 2. private只可以在类/友元中访问到. #include <iostream> using namespace std; //-- ...

  9. 谈Swift中的访问控制

    访问控制(Access Control) 访问控制可以限定其他源文件或模块中的代码对你的代码的访问级别.这个特性可以让我们隐藏代码的一些实现细节,并且可以指定一些代码和访问和使用的优先接口. 你可以明 ...

随机推荐

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. ExtJS 4.2 Date组件扩展:添加清除按钮

    ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...

  3. 利用Node.js的Net模块实现一个命令行多人聊天室

    1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...

  4. CGI与FastCGI nginx+PHP-FPM

    本文转载自CGI与FastCGI 1.当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html. ...

  5. 【Machine Learning】决策树案例:基于python的商品购买能力预测系统

    决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...

  6. 微信小程序(微信应用号)组件讲解

    这篇文章主要讲解微信小程序的组件. 首先,讲解新建项目.现在有句话:招聘三天以上微信小程序开发,这个估计只能去挖微信的工程师了.技术新,既然讲解,那我们就从开始建项目讲解. 打开微信web开发者工具, ...

  7. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  8. 文件随机读写专用类——RandomAccessFile

     RandomAccessFile类可以随机读取文件,但是在测试中并不好用;File类可以测试文件存不存在,不存在可以创建文件;FileWriter类可以对文件进行重写或者追加内容;FileReade ...

  9. Node.js入门

    开始之前,安利一本正在看的书<站在两个世界的边缘>,作者程浩,上帝丢给他太多理想,却忘了给他完成理想的时间.OK,有兴趣的可以看一看. node.js如标题一样,我也是刚开始接触,大家一起 ...

  10. MVVM大比拼之knockout.js源码精析

    简介 本文主要对源码和内部机制做较深如的分析,基础部分请参阅官网文档. knockout.js (以下简称 ko )是最早将 MVVM 引入到前端的重要功臣之一.目前版本已更新到 3 .相比同类主要有 ...