Created
February 26, 2022 14:00
-
-
Save arsalankhan994/e034fe9cd0ec2713a38fe672b873f304 to your computer and use it in GitHub Desktop.
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
fun main() { | |
/* | |
Primary Constructor Example | |
*/ | |
val primaryConstructorOnly = PrimaryConstructorOnly() | |
println("My name is ${primaryConstructorOnly.name}") | |
println("My age is ${primaryConstructorOnly.age}") | |
val primaryConstructorWithParams = PrimaryConstructorWithParams( | |
"Erselan Khan", | |
27 | |
) | |
println("My name is ${primaryConstructorWithParams.name}") | |
println("My age is ${primaryConstructorWithParams.age}") | |
/* | |
Secondary Constructor Example | |
*/ | |
val secondaryConstructor = SecondaryConstructor("Erselan Khan") | |
println("Secondary Constructor: My name is ${secondaryConstructor.name}") | |
println("Secondary Constructor: My age is ${secondaryConstructor.age}") | |
} | |
class PrimaryConstructorOnly() { | |
val name = "Erselan Khan" | |
val age = 27 | |
} | |
class PrimaryConstructorWithParams( | |
internal val name: String, | |
internal val age: Int | |
) { | |
} | |
class SecondaryConstructor() { | |
lateinit var name : String | |
var age = 27 | |
/* | |
Two secondary constructor | |
one for name and other for age | |
*/ | |
constructor(name: String): this() { | |
this.name = name | |
} | |
constructor(age: Int): this() { | |
this.age = age | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment