Created
April 29, 2019 07:30
-
-
Save Tenkei/66590eaf2d9adc758835906ea57ee949 to your computer and use it in GitHub Desktop.
Kotlin, what is class delegation all about
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
class Aircraft( | |
private val liftMechanism: Flyable, | |
private val propulsion: Movable | |
): Flyable, Movable { | |
override fun fly() { | |
liftMechanism.fly() | |
} | |
override fun move() { | |
propulsion.move() | |
} | |
} |
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
class Aircraft( | |
liftMechanism: Flyable, | |
propulsion: Movable | |
): Flyable by liftMechanism, Movable by propulsion | |
interface Flyable { | |
fun fly() | |
} | |
class FixedWing: Flyable { | |
override fun fly() = print("Adjust wing angle..") | |
} | |
class RotaryWing: Flyable { | |
override fun fly() = print("Start spinning the blades..") | |
} | |
class HotAir: Flyable { | |
override fun fly() = print("Start floating..") | |
} | |
interface Movable { | |
fun move() | |
} | |
class Unpowered: Movable { | |
override fun move() = print("Just hanging up here..") | |
} | |
class Propelled: Movable { | |
override fun move() = print("Starting engine..") | |
} |
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
interface Aircraft { | |
fun fly() | |
fun move () | |
} | |
interface WingedAircraft: Aircraft { | |
override fun fly() = print("Adjust wing angle..") | |
} | |
interface RotorCraft: Aircraft { | |
override fun fly() = print("Start spinning the blades..") | |
} | |
interface AerostatsAircraft: Aircraft { | |
override fun fly() = print("Start floating..") | |
} | |
interface UnpoweredVehicle: Aircraft { | |
override fun move() = print("Just hanging up here..") | |
} | |
interface PropelledVehicle: Aircraft { | |
override fun move() = print("Starting engine..") | |
} | |
class Glider: WingedAircraft, UnpoweredVehicle | |
class Jet: WingedAircraft, PropelledVehicle | |
class Gyroglider: RotorCraft, UnpoweredVehicle | |
class Helicopter: RotorCraft, PropelledVehicle | |
class Balloon: AerostatsAircraft, UnpoweredVehicle | |
class Zeppelin: AerostatsAircraft, PropelledVehicle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment