Created
July 8, 2019 13:19
-
-
Save myanmarlinks/f31d1e9fd8878202a2e06ad348d879ca to your computer and use it in GitHub Desktop.
Bridge to Kotlin Part 8
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(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