Swift 基礎學習(實例方法)

/*
   方法
(1) 實例方法
(2) 方法的參數名稱
(3) 實例方法中隱藏的self
(4) mutating方法
(5) 類方法
(6) 下標腳本語法
(7) 單索引值下標腳本
(8) 多索引值下標腳本
*/

//(1)實例方法
class MyPoint {
    var _x: Double = 0.0
    var _y: Double = 0.0
    //當我們定義實例方法時候,第一個參數爲內部參數,之後的參數即可作爲外部參數也可作爲內部參數,如果想要作爲內部參數,只需要在y前面加_即可
    func setX(x: Double, y: Double) {
        _x = x
        _y = y
    }
//    func setX(x: Double, _ y: Double) {
//        _x = x
//        _y = y
//    }
    func show() {
        print("x = \(_x), y = \(_y)")
    }
}
var p0 = MyPoint()
//通過實例對象調用方法
p0.setX(10.0, y: 10.0)
p0.show()

//(2)方法的參數名稱
func setX(x: Double, y: Double) {

}
setX(10, y: 10)

//(3)self
class MyPoint1 {
    var _x: Double = 0.0
    var _y: Double = 0.0
    func setX(x: Double, y: Double) {
//        _x = x
//        _y = y
        //等同於
        self._x = x
        self._y = y
    }
    func show() {
        print("x = \(_x), y = \(_y)")
    }
}
var p1 = MyPoint1()
p1.setX(10, y: 10) //p1 == self

/*
(4) mutating方法
值類型 (結構體或者枚舉) 默認方法是不可以修改屬性的,如果要修改需要做特殊處理
*/

class MyPerson {
    var name: String = ""
    var age: Int = 0
    func set(name name: String, age: Int) {
        self.name = name
        self.age = age
    }
    func show() {
        print("name = \(name), age = \(age)")
    }
}
var m0 = MyPerson()
m0.set(name: "Frank", age: 24)
m0.show()

//將class改爲struct
struct MyPerson1 {
    var name: String = ""
    var age: Int = 0
    //特殊處理在方法名添加關鍵字mutating
    mutating func set(name name: String, age: Int) {
        self.name = name
        self.age = age
    }
    func show() {
        print("name = \(name), age = \(age)")
    }
}
var m1 = MyPerson1()
m1.set(name: "Frank", age: 24)
m1.show()

enum LightSwitch {
    case OFF, LOW, HIGH
    mutating func next () {
        switch self {
        case .OFF:
            self = LOW
        case .LOW:
            self = HIGH
        case .HIGH:
            self = OFF

        }
    }
}
var light = LightSwitch.OFF
light.next() //light == .LOW

/*
(5) 類型方法
通過類名來調用的方法,就像類型屬性一樣.
類方法對應的關鍵字是static (結構體和枚舉) class (類)
類方法裏面不存在self
*/
struct TypeMethods {
    var p: Int = 0
    static var sp: Int = 0
    func method() {
        print("p = \(p), sp = \(TypeMethods.sp)")
    }
    //類方法裏面不可以訪問普通的成員變量
    static func staticMethod() {
//        print("p = \(p)")
        print("sp = \(TypeMethods.sp)")

    }
}
var tm = TypeMethods()
tm.method()
TypeMethods.staticMethod()

//跟結構體一樣
class TypeMethods1 {
    var p: Int = 0
    static var sp: Int = 0
    func method() {
        print("p = \(p), sp = \(TypeMethods.sp)")
    }
    //類方法裏面不可以訪問普通的成員變量
    class func staticMethod() {
//                print("p = \(p)")
        print("sp = \(TypeMethods.sp)")

    }
}
var tm1 = TypeMethods1()
tm1.method()
TypeMethods1.staticMethod()

/*
(6)subscripts(下標)-訪問對象中數據的快捷方式
所謂下標腳本語法就是能夠通過 實例[索引值]來訪問實例中的數據
subscript (index: Int) -> Element //數組
subscript (key: Key) -> Value? //字典
dict[key]
dict.subscript(key)
array[10]
array.subscript(10)
*/
let array = [1,3,5,6]
print(array[2]) // 實例對象[索引] subscripts
let dict = ["1":1] // key:value , key hash
print(dict["1"])  //

let array1:Array<Int> = [1,3,5,6]
print(array1[2]) // 實例對象[索引] subscripts
let dict1:Dictionary<String, Int> = ["1":1] // key:value , key hash
print(dict1["1"])

/*
(7)subscript方法實現
*/
struct Student {
    let name: String = ""
    var math: Int = 0
    var chinese: Int = 0
    var english: Int = 0
    func scoreOf(course: String) -> Int? {
        switch course {
            case "math":
            return math
            case "chinese":
            return chinese
            case  "english":
            return english
            default:
            return nil
        }
    }
    //想要實現下標訪問時需要實現自己定義的一個subscript
    subscript (course: String) -> Int? {
        get {
            switch course {
            case "math":
                return math
            case "chinese":
                return chinese
            case  "english":
                return english
            default:
                return nil
            }

        }
        set {
            switch course {
            case "math":
                 math = newValue!
            case "chinese":
                 chinese = newValue!
            case  "english":
                 english = newValue!
            default:
                print("key wrong")
            }

        }
    }


}
var Frackchun = Student(math: 98, chinese: 94, english: 45)
//Frackchun["math"] Frackchun.scoreOf("math")
//想要修改的話要實現setter方法
Frackchun["math"] = 99
print(Frackchun.scoreOf("math"))
print(Frackchun["math"])

/*
(8)多索引subscript
*/
struct Mul {
    subscript (a: Int, b: Int) -> Int {
        return a * b
    }
}
var mul = Mul()
print(mul[3, 5])
發佈了80 篇原創文章 · 獲贊 8 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章