2019年6月19日 星期三

method


import UIKit
//習慣上class名稱第一個字母大謝,屬性名稱第一個字母小寫
class Counter{
    var count = 0;
    //實體的method,只有實體才可以執行
    func increment(){
        count += 1;
    }
    func increment(by amount:Int){
        count += amount;
    }
    func reset(){
        count = 0;
    }
}
let counter = Counter();
counter.increment();
print(counter.count);
counter.increment(by: 5);
print(counter.count);
counter.reset();
print(counter.count);

//使用self的時機
/*
struct Point{
    var x = 0.0, y = 0.0;
    func isToTheRightOf(x:Double) -> Bool {
        return self.x > x;
    }
}
let SomePoint = Point(x: 4.0, y: 5.0);
if SomePoint.isToTheRightOf(x: 1.0) {
    print("比較大!");
}
*/
/*
struct Point{
    var x = 0.0, y = 0.0;
    //method要去改變store property的值時,必須加上mutating
   mutating func moveBy(x deltaX:Double, y deltaY:Double){
        x += deltaX
        y += deltaY
    }
}
*/
struct Point{
    var x = 0.0, y = 0.0;
    mutating func moveBy(x deltaX:Double, y deltaY:Double){
        self = Point(x: x+deltaX, y: y+deltaY);
    }
}
var somePoint = Point(x: 1.0, y: 1.0);
somePoint.moveBy(x: 2.0, y: 3.0);

enum TriStatsSwitch{
    case off, low, heigh
    mutating func next(){
        switch self {
        case .off:
            self = .low;
        case .low:
            self = .heigh;
        case .heigh:
            self = .off;
        }
    }
}
var openLight = TriStatsSwitch.low;
openLight.next();

struct levelTracker{
    static var highestUnlockedLevel = 1;
    var currentLevel = 1;
    
    static func unlock(_ level:Int){
        if level > highestUnlockedLevel {
            highestUnlockedLevel = level;
        }
    }
}