Let's take a look at example that follows. Here we will create an object of an anonymous class passed to method that expects it (AnonymousObject.kt):
abstract class Command {
abstract fun execute()
}
class Executor : Command() {
private val commands = mutableListOf<Command>()
fun addCommand(command: Command) {
commands.add(command)
}
override fun execute() {
for (command in commands) {
command.execute()
}
}
}
fun tryAnonymousObjects() {
val executor = Executor()
executor.addCommand(object : Command() {
override fun execute() {
println("Starting session.")
}
})
executor.addCommand(object : Command() {
override fun execute() {
println("Logging in.")
}
})
executor.addCommand(object : Command() {
override fun execute() {
println("Launch app 1")
}
})
executor.addCommand(object : Command() {
override fun execute() {
println("Launch app 2")
}
})
executor.addCommand(object : Command() {
override fun execute() {
println("Logging out")
}
})
executor.addCommand(object : Command() {
override fun execute() {
println("Stopping session")
}
})
executor.execute()
}
Console output:
Starting session.
Logging in.
Launch app 1
Launch app 2
Logging out
Stopping session
Let's take a look at another powerful example using object (MoreObject.kt):
// It is crucial to be 'open'!
open class Computer(cpuCores: Int, memoryInGigabytes: Int) {
open val cores: Int = cpuCores
open val memory: Int = memoryInGigabytes
}
interface Gpu {
fun displayImage()
}
interface SoundCard {
fun playSound()
}
fun tryMoreObject() {
val myComputer = object : Computer(8, 64), Gpu, SoundCard {
override fun displayImage() {
println("Displaying image.")
}
override fun playSound() {
println("Playing sound")
}
}
println("My computer [ cpu cores: ${myComputer.cores} ][ memory: ${myComputer.memory}GB ]")
myComputer.displayImage()
myComputer.playSound()
}
Console output:
My computer [ cpu cores: 8 ][ memory: 64GB ]
Displaying image.
Playing sound
In the following example, the simplest possible, we'll use an object just as a holder to store some values (SimpleObject.kt):
fun simpleObjectExample(){
val obj = object {
var x = 0
var y = 1
}
println("Object [ ${obj.x} ][ ${obj.y} ]")
obj.x = 10
obj.y = 11
println("Object [ ${obj.x} ][ ${obj.y} ]")
}
Console output:
Object [ 0 ][ 1 ]
Object [ 10 ][ 11 ]
Finally let's take a look at last example of using Kotlin object (ObjectVariablesAccess.kt):
fun countExecution(){
val executor = Executor()
var leftExecuted = 0
var rightExecuted = 0
val left = object: Command(){
override fun execute() {
println("LEFT")
leftExecuted++
}
}
val right = object: Command(){
override fun execute() {
println("RIGHT")
rightExecuted++
}
}
executor.addCommand(left)
executor.addCommand(left)
executor.addCommand(left)
executor.addCommand(right)
executor.addCommand(right)
executor.addCommand(left)
println("LEFT vs RIGHT, [ ${leftExecuted} ][ ${rightExecuted} ]")
executor.execute()
println("LEFT vs RIGHT, [ ${leftExecuted} ][ ${rightExecuted} ]")
}
Console output:
LEFT vs RIGHT, [ 0 ][ 0 ]
LEFT
LEFT
LEFT
RIGHT
RIGHT
LEFT
LEFT vs RIGHT, [ 4 ][ 2 ]
As you can see we can access to variables of enclosed scope.