当前位置: 首页 > news >正文

[Swift学习] 访问控制 Access Control, private、public、filePrivate等修饰符

private , fileprivate,internal,public,open

从整体看,权限 private < fileprivate < internal < public < open

swift官方定义

Swift Access Control

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, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

Private

private是这些修饰词中,访问权限最严格的。private用来修饰类和方法或者属性时,该类、属性、方法无法在该类以外使用,比如修饰类时,其他类无法使用该类

在这里插入图片描述
修饰方法时,其他类无法使用该方法
在这里插入图片描述
修饰属性时,其他类无法使用该属性,读取也不行

在这里插入图片描述
这里其实稍微有个悖论,如果用private来修饰类时,其他类都无法使用该类,那这个类的设计目的是什么呢?只能自调用?

fileprivate

fileprivate这个修饰词挺有意思,它意味着修饰的东西只能在该文件中访问,而不能在其他文件中访问(我学过的语言中,只有swift是这么设计的,但具体为什么设计基于文件的访问权限,我没有查到资料 )

比如在A.swift中,其他类可以访问该类,而在B.swift中是无法访问filePrivateClass的
请添加图片描述
请添加图片描述

internal

internal是默认的修饰词,在该模块内部可以被访问。 如果是封装库,则整个库都可以访问该类,而库外的代码是无法访问的。 如果是App中的类,则整个App都可以访问该类

Public 和 Open

这两个修饰词之所以放在一起,是因为他们的属性很相似,都是在模块的外部可以访问。 但是Public的修饰内容在外部无法被继承或者重写,而Open可以

使用原则

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.
不能用一个更低访问级别的实体来定义当前实体

这句话看着不太好懂,我觉得可以这么理解,不要破坏一个类的权限,比如类filePrivateClass是fileprivate,如果你用public修饰,则破坏了它的权限,因为其根本属性时fileprivate,用public修饰就让编译器不知道如何来编译了
请添加图片描述

Getter和setter

我们使用Objective-C的时候,通常会用readOnly等修饰词来控制读写权限,而在swift中,我们可以通过private修饰set和get做到

可读写

默认的权限internal即可

class Person : NSObject {
    var name : String? = nil
}

不可读

class Person : NSObject {
    private var name : String? = nil
}

不可以写

class Person : NSObject {
    private(set) var name : String? = nil
}

相关文章:

  • [SQL]数据库语言学习
  • 软考高频考点——项目中标了以后该怎么做?
  • 基于注意力机制的LSTM液体管道非稳态工况检测
  • Oracle VM VirtualBox安装CentOS 7系统
  • windows hello人脸识别设置没反应的解决办法
  • 企业级容器云PaaS解决方案【厚PaaS+轻应用+微服务】---(2)
  • dubbo 利用分组区分同一个服务的不同实现
  • npm下载的包分类
  • 在绩效评估中使用 360 反馈
  • Object Detection in Optical Remote Sensing Images: A Survey and A New Benchmark
  • 学会这招,轻松实现批量PDF转jpg,快来码住
  • GIS海量数据前端加载优化策略
  • 自组织是管理者和成员的双向奔赴
  • shell脚本使用sftp上传文件
  • PreparedStatement,根据条件查询总数
  • -------------------- 第二讲-------- 第一节------在此给出链表的基本操作
  • create-react-app项目添加less配置
  • Django 博客开发教程 8 - 博客文章详情页
  • gitlab-ci配置详解(一)
  • IndexedDB
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • Python十分钟制作属于你自己的个性logo
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • TypeScript实现数据结构(一)栈,队列,链表
  • windows下mongoDB的环境配置
  • 类orAPI - 收藏集 - 掘金
  • 七牛云假注销小指南
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 微信小程序:实现悬浮返回和分享按钮
  • 用 Swift 编写面向协议的视图
  • 正则与JS中的正则
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • 06-01 点餐小程序前台界面搭建
  • #stm32驱动外设模块总结w5500模块
  • #控制台大学课堂点名问题_课堂随机点名
  • (arch)linux 转换文件编码格式
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (js)循环条件满足时终止循环
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (十一)图像的罗伯特梯度锐化
  • (实战篇)如何缓存数据
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (转)Mysql的优化设置
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...