Swift学习之方法

来源:互联网 发布:算法设计与分析陈慧南 编辑:程序博客网 时间:2024/05/24 00:34

Method :方法

1.实例方法

class Counter {    var count = 0    func increment(){        ++count    }    func add(value:Int){        count += value    }    func reSet()->Int{        count = 0        return count    }}let counter = Counter()counter.increment()print(counter.count) //1counter.add(3)print(counter.count) //4counter.reSet()  //0struct Point {    var x = 0.0 , y = 0.0    mutating func modifySelf(xValue:Double,withY yValue:Double){        self = Point(x: xValue, y: yValue)    }}var point = Point(x: 2, y: 2)point.modifySelf(3, withY: 4)print(point.x) // 3.0

2.类型方法 :在func前面加static,用类型本身来调用该方法

class SomeClass {    static func someFunc(){        print("This is a static fuc")    }}SomeClass.someFunc() //"This is a static fuc"
0 0
原创粉丝点击