-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding data classes with md file for tutorial purposes
- Loading branch information
1 parent
7c0ab72
commit 5985d15
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|