I've got following example class:
fun main(args: Array<String>) {
var car = Car("Ford", 50, 8000)
println(car.vendor)
println("Speed: ${car.speed}")
car.speed = 65 // How does it know which setter to invoke?
println("New speed: ${car.speed}")
}
class Car(vendor: String = "Unknown", speed: Int = 0, price: Int = 0) {
var vendor = vendor
get() = field
set(newVendor: String) {
field = newVendor
}
var speed = speed
get() = field
set(speed: Int) {
field = speed
}
var price = price
get() = field
set(newPrice: Int) {
field = price
}
}
When I change the speed-attribute (please see the commented line): Where does Kotlin know from, which setter-method it has actually to invoke?
There are two setter-methods within my class (speed, price) which both are named "set" and both expect an integer-value.
Is the order, in which the methods are defined, crucial?
The respective getter-/setter-methods have to be written directly after the attribute-definition? Or does it somehow work differently? If so: How?
Are the indentations just a covention? Or are the indentations needed for the compiler?