Created
May 12, 2018 19:38
-
-
Save uberto/63a9ec188e347296898764c0cddb1a96 to your computer and use it in GitHub Desktop.
Multiple Inheritance via Delegation in 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
package com.gamasoft.delegation | |
import assertk.assert | |
import assertk.assertions.isEqualTo | |
import org.junit.Test | |
//https://kotlinlang.org/docs/reference/delegation.html | |
interface Printer { | |
fun print(msg: String) | |
} | |
class PrinterStdOut() : Printer { | |
override fun print(msg: String) = println(msg) | |
} | |
interface Scanner { | |
fun scan(): String | |
} | |
class ScannerConst(val c: String) : Scanner { | |
override fun scan(): String = c | |
} | |
class AlsoPrinter(p: Printer) : Printer by p { | |
fun otherMethod(){ | |
///do something else | |
} | |
} | |
class MultiFunction(p: Printer, s: Scanner) : Printer by p, Scanner by s | |
internal class DelegationTest { | |
@Test | |
fun delegateInheritance() { | |
val ap = AlsoPrinter(PrinterStdOut()) | |
ap.print("hello") | |
assert(ap is Printer) | |
} | |
@Test | |
fun multipleInheritance() { | |
val mf = MultiFunction(PrinterStdOut(), ScannerConst("hello")) | |
val msg = mf.scan() | |
assert( msg).isEqualTo("hello") | |
mf.print(msg) | |
assert(mf is Scanner) | |
assert(mf is Printer) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment