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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章