zoukankan      html  css  js  c++  java
  • Swift

    通过类的计算属性set和get,我们可以对索引的加减进行保护。下面是一个样例,索引index初始值是0,有效范围是0~2。不管是index++还是index--,索引都是一直在这个范围能循环遍历。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Test {
        var _index = 0
        var index:Int {
            get{
                return _index
            }
            set{
                _index = newValue
                if _index < 0 {
                    _index += 3
                }else if _index > 2 {
                    _index -=3
                }
            }
        }
         
        func onNext(){
            index++
        }
         
        func onPre(){
            index--
        }
    }
  • 相关阅读:
    POJ 1475 推箱
    POJ 2253 Frogger
    POJ 1970 The Game
    POJ 1979 Red and Black
    HDU 1546 Idiomatic Phrases Game 求助!help!!!
    Fibonacci 1
    BZOJ 1041
    椭圆曲线质因数分解
    奇怪的高精度
    数论v2
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4838617.html
Copyright © 2011-2022 走看看