Swift-下标(subscript)

2020-12-13  本文已影响0人  lieon

下标(subscript)

class Point {
    var x = 0.0, y = 0.0
    
    subscript(index: Int) -> Double {
        set {
            if index == 0 {
                x = newValue
            } else if index == 1 {
                y = newValue
            }
        }
        get {
            if index == 0 {
                return x
            } else if index == 1 {
                return y
            }
            return 0
        }
    }
}

var point = Point()
point[0] = 11
point[1] = 23
上一篇下一篇

猜你喜欢

热点阅读