Swift学习笔记方法

class Counter {
    var count = 0
    func increment(){
        count++
    }
    func incrementBy(amount: Int) {
        count += amount
    }
    func reset() {
        count = 0
    }
    func increment(amount: Int,numberOfTime: Int)
    {
        self.count++
    }
}

let counter = Counter()
counter.increment()
counter.incrementBy(5)
counter.reset()
counter.increment(100, numberOfTime: 10)

struct Point {
    var x = 0.0,y = 0.0
    mutating func moveByX(deltaX: Double, y deltay:Double){
        self = Point(x: x + deltaX, y: y + deltay)
    }
    
}
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveByX(2.0, y: 3.0)

enum TriStateSwitch{
    case Off, Low, High
    mutating func next(){
        switch self{
        case Off:
            self = Low
        case Low:
            self = High
        case High:
            self = Off
        }
    }
}
var ovenLight = TriStateSwitch.Low
ovenLight.next()
ovenLight.next()

class SomeClass {
    class func someTypeMethod(){
    }
}

SomeClass.someTypeMethod()

swift定义方法里要注意与其他语言不同的地方,如结构里要修改本身的值就要使用到变异方法mutating

在类里定义类型方法(静态方法)使用关键字 class

你可能感兴趣的:(swift)