Skip to content

Instantly share code, notes, and snippets.

@Tenkei
Created April 29, 2019 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tenkei/66590eaf2d9adc758835906ea57ee949 to your computer and use it in GitHub Desktop.
Save Tenkei/66590eaf2d9adc758835906ea57ee949 to your computer and use it in GitHub Desktop.
Kotlin, what is class delegation all about
class Aircraft(
private val liftMechanism: Flyable,
private val propulsion: Movable
): Flyable, Movable {
override fun fly() {
liftMechanism.fly()
}
override fun move() {
propulsion.move()
}
}
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..")
}
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