5.1 Singleton Classes
Singleton Class Attributes:
Rules for making a class Singleton
Implementation
object Counter {
var counter: Int
// Only allows retrival of the counter,
// setting restricted to only within Singleton
private set
// Optional init block if you need some initalization
init {
counter = 0
}
// Increments the counter and returns the new number after incrementing.
fun incrementCounter(): Int {
return ++counter
}
// Can define whatever member functions you want!
}What if we want to take in arguments?
Last updated
Was this helpful?