Skip to content

Instantly share code, notes, and snippets.

@myanmarlinks
Created July 8, 2019 13:19
Show Gist options
  • Save myanmarlinks/f31d1e9fd8878202a2e06ad348d879ca to your computer and use it in GitHub Desktop.
Save myanmarlinks/f31d1e9fd8878202a2e06ad348d879ca to your computer and use it in GitHub Desktop.
Bridge to Kotlin Part 8
fun main(args: Array<String>) {
val mammal: Human = Human("John Doe","CEO")
println(greetMammal(mammal))
val cat = Cat("Shwe War")
println(greetMammal(cat))
}
fun greetMammal(mammal: Mammal): String {
when(mammal) {
is Human -> return "Hello ${mammal.name}; You're working as a ${mammal.job}"
is Cat -> return "Hello ${mammal.name}"
}
}
sealed class Mammal(val name: String)
class Cat(val catName: String) : Mammal(catName)
class Human(val humanName: String, val job: String) : Mammal(humanName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment