Created
September 24, 2016 00:13
-
-
Save nsomar/ecf09507332ee0b467b95f56361b1b0d to your computer and use it in GitHub Desktop.
Implementing a counter as a class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Impelentation Counter as a class | |
| class Counter { | |
| private var x: Int | |
| init(number: Int) { | |
| x = number | |
| } | |
| func setNumber(_ number: Int) { | |
| x = number | |
| } | |
| func getNumber() -> Int { | |
| return x | |
| } | |
| func increment() { | |
| x += 1 | |
| } | |
| func decrement() { | |
| x -= 1 | |
| } | |
| } | |
| let c = Counter(number: 1) | |
| print(c.getNumber()) | |
| c.increment() | |
| print(c.getNumber()) | |
| c.decrement() | |
| print(c.getNumber()) | |
| c.setNumber(20) | |
| print(c.getNumber()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment