How to use instance methods in class with Swift

1 Answer

0 votes
class Test {
    var count = 0
    func increase() {
        count += 1
    }
    func increase(by n: Int) {
        count += n
    }
    func reset() {
        count = 0
    }
    func show() {
        print(count) 
    }
}

let obj = Test()
obj.show()

obj.increase()
obj.show()
    
obj.increase(by: 3)
obj.show()

obj.reset()
obj.show()




/*
run:

0
1
4
0

*/

 



answered Feb 18, 2021 by avibootz
edited Feb 18, 2021 by avibootz
...