Kotlin Data Class
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Many times in the real world, we create classes solely for the purpose of holding data and nothing else. Kotlin provides a compact way of creating such classes called Data class.
Example:- Customer class, Employee class etc.
Data classes in Kotlin allow us to create class with only data.
Example of Data class in Kotlin.
In the following example, we created a data class Employee with employee Id and employee Name. We are then able to create and access the employee object just like a regular class object.
data class Employee(val empId: Int, val empName: String)
fun main(args: Array<String>)
{
var employee = Employee(1723, "Mugambo")
println(employee.empId)
println(employee.empName)
}
Output:
1723
Mugambo
Kotlin itself will provide the following functions automatically for data classes.
1. toString implementation.
2. equals and hashCode implementation.
3. Component functions.
4. Copy function.