diff --git a/src/halil/raso/com/tutorials/dataclasses/dataclass.kt b/src/halil/raso/com/tutorials/dataclasses/dataclass.kt new file mode 100644 index 0000000..d0a5c05 --- /dev/null +++ b/src/halil/raso/com/tutorials/dataclasses/dataclass.kt @@ -0,0 +1,14 @@ +package halil.raso.com.tutorials.dataclasses + +fun main(args : Array) { //entry point of any kotlin program. + val usr1 = User(name = "Halil", age = 32) // we create an object of a data class User + println(usr1.toString()) // This is equivalent to println(user1). + val usr2 = usr1.copy(name= "Khalil", age = 31) + println(usr2) // Will print: User(name=Halil, age=31) + val usr3 = usr1; // Equivalent to copy mehtod. + usr3.age= 33; + println(usr3) // Will print: User(name=Halil, age=33) + println(usr3.component1())// Getting the first parameter of object. +} + +data class User(val name: String, var age: Int) \ No newline at end of file diff --git a/src/halil/raso/com/tutorials/dataclasses/dataclasses.md b/src/halil/raso/com/tutorials/dataclasses/dataclasses.md new file mode 100644 index 0000000..6f6d76a --- /dev/null +++ b/src/halil/raso/com/tutorials/dataclasses/dataclasses.md @@ -0,0 +1,16 @@ +Most of classes we defined or used are for storing data purposes. +For those classes are define in general: +1- A constructor. +2- Fields to store data. +3- getter and setter methods. +4- hashCode(), equals and toString() methods. +Data Class of Kotlin comes in scene and rescue us from doing such boring tasks. +It will useful to think Kotlin data class as Java beans. +General syntax of defining such classes is as following: +data class Class_name(val/var param1, ...., val/var paramn). +Important Note: If you are only interested in getter, you should define +the corresponding parameter as val not var. +* If you are interested in getting the first parameters without regarding +its name, you could use component1() function. This is called destruction. + + \ No newline at end of file