Learn Kotlin Quickly - Constructors And Properties
Publish date: Mar 25, 2019
Constructor
class Person (private var name: String, private var age: Int){
init {
println("Person $name was created!")
}
constructor(name: String): this(name, Random().nextInt()){
println("Secondary contructor was invoked!")
}
fun describeYourself() = println("My name is $name and I am $age years old.")
}
Properties
class Computer{
var cost = 500f
val name = "Apple"
lateinit var currentUser: String
val isExpensive : Boolean
get() = cost > 1000
var properties: String = ""
set(value) {
field = value
println("Properties were changed")
}
var id = 10
private set
init {
this.id = 5
}
}
Disqus comments are disabled.