zoukankan      html  css  js  c++  java
  • swift 下标脚本

    下标脚本 可以定义在类(Class)、结构体(structure)和枚举(enumeration)这些目标中,可以让这些类型的实例通过[]快速访问属性和方法.

      subscript(index: Int) -> Int {
        get {
          // 返回与入参匹配的Int类型的值
        }
    
        set(newValue) {
          // 执行赋值操作
        }
    }
    

    通常下标脚本是用来访问集合(collection),列表(list)或序列(sequence)中元素的快捷方式

    struct Matrix {
        let rows: Int, columns: Int
        var grid: Double[]
        init(rows: Int, columns: Int) {
          self.rows = rows
          self.columns = columns
          grid = Array(count: rows * columns, repeatedValue: 0.0)
        }
        func indexIsValidForRow(row: Int, column: Int) -> Bool {
            return row >= 0 && row < rows && column >= 0 && column < columns
        }
        subscript(row: Int, column: Int) -> Double {
            get {
                assert(indexIsValidForRow(row, column: column), "Index out of range")
                return grid[(row * columns) + column]
            }
            set {
                assert(indexIsValidForRow(row, column: column), "Index out of range")
                grid[(row * columns) + column] = newValue
            }
        }
    }
    
    var matrix = Matrix(rows: 2, columns: 2)
    
    let someValue = matrix[2, 2]
    
  • 相关阅读:
    Thinkphp注释
    THINKPHP5 如何在 控制器内调用model模型
    thinkphp5路由定义
    Thinkphp5读取当前config配置文件
    thinkphp5计算代码块的性能
    thinkphp的执行流程
    php filter过滤器
    nginx在收到stop信号后的处理
    寻找重复数
    奶牛和公牛
  • 原文地址:https://www.cnblogs.com/zhepama/p/3857585.html
Copyright © 2011-2022 走看看