美文网首页
open public internal fileprivate

open public internal fileprivate

作者: Lucky_1122 | 来源:发表于2020-04-26 11:44 被阅读0次

权限级别
open > public >internal > fileprivate > private

1.open 修饰 在本Module和跨Module 都可以访问 可以被继承和重写
2.public 修饰 在本Module和跨Module都可以访问,但是跨Module的时候是不能被继承和重写的 只能访问
3.internal 修饰 在本Module内都可访问,跨Module不可访问
4.fileprivate private 修饰 在当前类中都可以访问 不同的是fileprivate 只要在当前文件中都可以访问 ,private 只能在本类中访问 (
\color{red}{注 在扩展类中 Swift4之后都可以访问 Swift4之前 private 在扩展类是不能访问的}
如下代码:

//在同一文件内
class ViewController: UIViewController {
    var testA:String?
    private var testB:String?
    fileprivate var testC:String?
}
extension ViewController {
    func aTest()  {
        testA = ""
        testB = ""
        testC = ""
    }
}
class AA: UIViewController {
    override func viewDidLoad() {
        let vc = ViewController()
        vc.testC = ""
        vc.testA = ""
//        vc.testB = "" 报错如下:'testB' is inaccessible due to 'private' protection level
    }
}

相关文章

网友评论

      本文标题:open public internal fileprivate

      本文链接:https://www.haomeiwen.com/subject/sgddwhtx.html