Skip to content

Instantly share code, notes, and snippets.

@simrandotdev
Last active December 24, 2018 00:28
Show Gist options
  • Save simrandotdev/1f0c9001da585118441d0dacdfe28ba3 to your computer and use it in GitHub Desktop.
Save simrandotdev/1f0c9001da585118441d0dacdfe28ba3 to your computer and use it in GitHub Desktop.
ClassesPropertiesAndMethodsInKotlin
fun main(args: Array<String>) {
val simran = Person("Simran", "Singh")
simran.greet()
simran.completeAddress("Mohali", "Punjab", zip = "160063", country = "India")
// As the 'getCompleteAddress()' is returning an Optional String, we are using 'elvis' operator to check for
// 'null' value, and if it is null than use some default method.
println(simran.getCompleteAddress() ?: "Unknown")
// Another way of doing this is using 'let' expression
simran.getCompleteAddress().let {
println(it)
}
val john = Person("John")
// john.firstName = "Jon" // You cannot do this as firstName is defined as 'val' not 'var'
john.lastName = "Doe"
john.greet()
val unknownPerson = Person()
unknownPerson.greet()
// Creating a 'Person' object with named parameters
// We can change the order of parameters if we use named parameters in a constructor or even in a method.
val joey = Person(lastName = "Tribiani", firstName = "Joey")
joey.greet()
joey.greetCompletely()
}
// Defining a class 'Person' with 2 properties: 'firstName' and 'lastName'
// 'firstName' is an immutable property, which means it cannot be changed once it is assigned a value.
// 'lastName' is an mutable property, which means is can be changed once it is assigned a value.
// Person has 2 constructors.
// First constructor takes 'firstName' and 'lastName', but 'lastName' is optional.
// If while creating 'Person' object we do not pass 'lastName' an empty string value will be assigned to it.
class Person(val firstName: String, var lastName: String = "") {
// You can define private properties that you want to set later through some other operation
private var completeAddress: String? = null
// The lateinit keyword stands for late initialization. Lateinit comes very handy when a
// non-null initializer cannot be supplied in the constructor,
// but the developer is certain that the variable will not be null when accessing it,
// thus avoiding null checks when referencing it later.
// https://www.kotlindevelopment.com/lateinit-kotlin/
private lateinit var fullName: String
// Second constructor is an empty constructor, which passes a value of 'Unknown' to 'firstName' and 'lastName'
// Giving a empty constructor which will call the upper constructor by default.
constructor() : this("Unknown", "Unknown")
init {
setupProperties()
}
fun greet() {
println("Hello $firstName")
}
fun greetCompletely() {
// From Kotlin 1.2 you can do this Check on 'lateinit' properties.
if (::fullName.isInitialized) {
println("Hello $fullName")
}
}
fun completeAddress(city: String, state: String, country: String, zip: String) {
completeAddress = "$city, $state" +
"\n" +
"$country - $zip"
}
// This is another shorter to write methods that has only one single return statement.
// You do not need to define return type neither braces. Just '=' after the method definition
// and the return statement
fun getCompleteAddress() = completeAddress
private fun setupProperties() {
fullName = "$firstName $lastName"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment