Skip to content

Commit

Permalink
Adding data classes with md file for tutorial purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
halil-raso committed Jan 3, 2018
1 parent 7c0ab72 commit 5985d15
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/halil/raso/com/tutorials/dataclasses/dataclass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package halil.raso.com.tutorials.dataclasses

fun main(args : Array<String>) { //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)
16 changes: 16 additions & 0 deletions src/halil/raso/com/tutorials/dataclasses/dataclasses.md
Original file line number Diff line number Diff line change
@@ -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.


0 comments on commit 5985d15

Please sign in to comment.