Skip to content

Instantly share code, notes, and snippets.

@arsalankhan994
Created February 26, 2022 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsalankhan994/e034fe9cd0ec2713a38fe672b873f304 to your computer and use it in GitHub Desktop.
Save arsalankhan994/e034fe9cd0ec2713a38fe672b873f304 to your computer and use it in GitHub Desktop.
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