Swift-Private/FilePrivate/Public/Open的作用和区别

原来 Swift 中有2种常用访问控制关键字(访问控制修饰符),分别为 private 和 public。而在 Swift 3.0+,又在原来的基础上新增了两种:fileprivate、open。它们可以看成是对 private 和 public 的进一步细分。


用一张图区分四种修饰符的访问权限

这里写图片描述


1、private 修饰符

只允许在当前类中调用,不包括 Extension
private 现在变为了真正的私有访问控制
用 private 修饰的方法不可以被代码域之外的地方访问


2、fileprivate 修饰符

fileprivate 其实就是过去的 private。
其修饰的属性或者方法只能在当前的 Swift 源文件里可以访问。
即在同一个文件中,所有的 fileprivate 方法属性都是可以访问到的。

class A {
    fileprivate func test(){
        print("this is fileprivate func!")
    }
}

class B:A {
    func show(){
        test()
    }
}

3、public 修饰符

修饰的属性或者方法可以在其他作用域被访问
但不能在重载 override 中被访问
也不能在继承方法中的 Extension 中被访问


4、open 修饰符

open 其实就是过去的 public,过去 public 有两个作用:

修饰的属性或者方法可以在其他作用域被访问
修饰的属性或者方法可以在其他作用域被继承或重载 override


从高到低排序如下:

open > public > fileprivate > private

Network Security: Private Communication in a Public World, Second Edition By Charlie Kaufman, Radia Perlman, Mike Speciner ............................................... Publisher: Prentice Hall Pub Date: April 22, 2002 Print ISBN-10: 0-13-046019-2 Print ISBN-13: 978-0-13-046019-6 Web ISBN-10: 0-13-715588-3 Web ISBN-13: 978-0-13-715588-0 Pages: 752 Copyright The Radia Perlman Series in Computer Networking and Security Acknowledgments Chapter 1. Introduction Section 1.1. Roadmap to the Book Section 1.2. What Type of Book Is This? Section 1.3. Terminology Section 1.4. Notation Section 1.5. Primer on Networking Section 1.6. Active vs. Passive Attacks Section 1.7. Layers and Cryptography Section 1.8. Authorization Section 1.9. Tempest Section 1.10. Key Escrow for Law Enforcement Section 1.11. Key Escrow for Careless Users Section 1.12. Viruses, Worms, Trojan Horses Section 1.13. The Multi-Level Model of Security Section 1.14. Legal Issues Part 1: Cryptography Chapter 2. Introduction to Cryptography Section 2.1. What Is Cryptography? Section 2.2. Breaking an Encryption Scheme Section 2.3. Types of Cryptographic Functions Section 2.4. Secret Key Cryptography Section 2.5. Public Key Cryptography Section 2.6. Hash Algorithms Section 2.7. Homework Chapter 3. Secret Key Cryptography Section 3.1. Introduction Section 3.2. Generic Block Encryption Section 3.3. Data Encryption Standard (DES) Section 3.4. International Data Encryption Algorithm (IDEA) Section 3.5. Advanced Encryption Standard (AES) Section 3.6. RC4 Section 3.7. Homework Chapter 4. Modes of Operation Section 4.1. Introduction Section 4.2. Encry
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值