Kotlin Properties and Fields
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
A Kotlin class can have properties.
Properties are of two types.
1. var properties
These properties can be changed once initialized.
2. val properties
These properties cannot be changed once initialized.
Example
class Employee
{
var empName: String = "Robert"
val deptName: String = "Mugambo"
}
fun main(args: Array<String>)
{
val emp1 = Employee()
println(emp1.empName)
println(emp1.deptName)
}
Output:
Robert
Mugambo
Properties can have getter and setter methods.
get method is used to get value of the property.
set method is used to set value to the property.
Example:
class Employee
{
var empName: String = ""
get() = field.toString()
set(value)
{
field = value.toUpperCase()
}
}
fun main(args: Array<String>)
{
val emp1 = Employee()
emp1.empName = "Mugambo"
println(emp1.empName)
}
Output:
MUGAMBO
We have used the get and set empName property as shown below.
fun main(args: Array<String>)
{
val emp1 = Employee()
emp1.empName = "Mugambo"
println(emp1.empName)
}