20-访问控制

2023-04-16  本文已影响0人  二斤寂寞

访问控制(Access Control)

□ 绝大部分实体默认都是internal 级别

访问级别的使用准则

元组类型

internal struct Dog {} 
fileprivate class Person {}                                                 
// (Dog, Person)的访问级别是fileprivate 
fileprivate var data1: (Dog, Person) 
private var data2: (Dog, Person) 

泛型类型

internal class Car {}
fileprivate class Dog {}
public class Person<T1, T2> {}
                                                 
// Person<Car, Dog>的访问级别是fileprivate 
fileprivate var p = Person<Car, Dog>() 

成员、嵌套类型

public class PublicClass {
    public var p1 = 0 // public
    var p2 = 0 // internal
    fileprivate func f1() {} // fileprivate 
    private func f2() {} // private                                              
} 
                                                 
class InternalClass { // internal
    var p = 0 // internal
    fileprivate func f1() {} // fileprivate 
    private func f2() {} // private                                              
} 
fileprivate class FilePrivateClass { // fileprivate 
    func f1() {} // fileprivate
    private func f2() {} // private                                              
} 
                                                 
private class PrivateClass { // private 
    func f() {} // private                                              
} 

成员的重写

public class Person {
  private var age: Int = 0
}

public class Student : Person {
  override var age: Int {                                            
    set {}                                              
    get {10} }                                              
  } 
} 
public class Person {
    private var age: Int = 0
    public class Student : Person {
          override var age: Int {                                            
              set {}                                              
              get {10} }                                              
          } 
    }
}                                             

下面代码能否编译通过?

private class Person {}
fileprivate class Student : Person {}
private struct Dog {
    var age: Int = 0
    func run() {}
}

fileprivate struct Person {
    var dog: Dog = Dog()
    mutating func walk() {                                           
        dog.run()                                          
        dog.age = 1 
    }
} 
private struct Dog {
    private var age: Int = 0
    private func run() {}                                      
} 

fileprivate struct Person {
    var dog: Dog = Dog()
    mutating func walk() {                                           
        dog.run()                                          
        dog.age = 1 
    }
}

getter、setter

fileprivate(set) public var num = 10
class Person {
   private(set) var age = 0
   fileprivate(set) public var weight: Int {                                              
        set {}                                          
        get { 10 } 
   } 
   internal(set) public subscript(index: Int) -> Int {
        set {}
        get { index } 
    }                                              
} 

初始化器

□ 如果结构体有private\fileprivate的存储实例属性,那么它的成员初始化器也是private\fileprivate
□ 否则默认就是internal

枚举类型的case

协议

□ 协议中定义的要求自动接收协议的访问级别,不能单独设置访问级别
□ public协议定义的要求也是public

□ 协议实现的访问级别必须 ≥ 类型的访问级别,或者 ≥ 协议的访问级别
□ 下面代码能编译通过么?

public protocol Runnable {
   func run()                                                
} 

public class Person : Runnable {
   func run() {}                                              
} 

扩展

扩展

public class Person {
    private func run0() {}
    private func eat0() {                                           
        run1() 
    } 
}
extension Person {
    private func run1() {}
    private func eat1() {
        run0() 
    }
} 
extension Person {
    private func eat2() {  
        run1() 
    }
}  

将方法赋值给var\let

struct Person {
  var age: Int
  func run(_ v: Int) { print("func run", age, v) }
    static func run(_ v: Int) { print("static func run", v) }
}
let fn1 = Person.run
fn1(10) // static func run 10

let fn2: (Int) -> () = Person.run
fn2(20) // static func run 20

let fn3: (Person) -> ((Int) -> ()) = Person.run
fn3(Person(age: 18))(30) // func run 18 30
上一篇下一篇

猜你喜欢

热点阅读