Created
February 15, 2019 05:58
-
-
Save theapache64/5a021e4de5a01019962ff191ee121cd7 to your computer and use it in GitHub Desktop.
Delegation Pattern `by` Kotlin
This file contains hidden or 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
interface Car { | |
fun carMethod(); | |
} | |
interface Bus { | |
fun busMethod(); | |
} | |
class Leyland : Bus { | |
override fun busMethod() { | |
println("Bus method") | |
} | |
} | |
class BMW : Car { | |
override fun carMethod() { | |
println("Car method") | |
} | |
} | |
class Combination(c: Car, b: Bus) : Car by c, Bus by b | |
fun main(args: Array<String>) { | |
val busPlusCar = Combination(BMW(), Leyland()) | |
busPlusCar.busMethod() | |
busPlusCar.carMethod() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment