-
-
Save albodelu/38a5b8e73205ce42051813e4f74c86de to your computer and use it in GitHub Desktop.
Kotlin Delegation Example
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
| class ByDouble(incrementer: Incrementer) : Incrementer by incrementer { | |
| override fun increment(number: Int): Int { | |
| return super.increment(number) * 2 | |
| } | |
| } |
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
| class ByTwo : Incrementer { | |
| override val by: (Int) -> Int = { it + 2 } | |
| } |
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 Incrementer { | |
| val by: (Int) -> Int | |
| fun increment(number: Int): Int { | |
| return by(number) | |
| } | |
| } |
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
| ByDouble(ByTwo()).increment(10) | |
| //devuelve 24 -> (10+2) * 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment