swift 4.0 全局变量局部变量方法下标

2018-01-26  本文已影响18人  透支未来

import UIKit


struct SomeSturct {
    static var storedTypeP = "Some Value"
    static var computedTypeProperty:Int {return 1}
}

enum SomeEnum {
    static var storedTypeP = "Some Value"
    static var computedTypeProperty:Int {return 6}
}


class SomeClass {
    static var storedTypeP = "Some Value"
    static var computedTypeProperty:Int {return 29}
    class var overrideComputedTypeP: Int {
        return 107
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        print(SomeSturct.storedTypeP)
//        print(SomeSturct.computedTypeProperty)
//        print(SomeClass.computedTypeProperty)
//        print(SomeClass.overrideComputedTypeP)
        
        
        let TimerOfFive = TimberOfNum(num:5)
        
        print(TimerOfFive[3])
        print(TimerOfFive[4,5])
        
        var newStudent = Student()
        newStudent.sayHello()
        
        
        //类型方法
//        var d = -12.3
//        if d.isLess(than: 0) {
//            d = Double.abs(d) //类方法
//        }
    }
}



class Student{
    //实例方法
    func sayHello() {
        print("hello")
    }
}
//在结构体或者枚举中使用static 在泪中使用class

//下标 subscript
struct TimberOfNum {
    let num:Int //作为结构体的常量属性
    subscript(index:Int)->Int{//定义一个下标,返回他和外界传入参数的乘积
        return num * index
    }
    
    subscript(index:Int,index2:Int)-> Int{
        return num * index+index2
    }
    
}


上一篇下一篇

猜你喜欢

热点阅读