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
val listener = object: OnMenuClickListener, OnMenuExpandListener { | |
override fun onMenuClick(item: Menu) { ... } | |
override fun onMenuExpand(item: Menu) { ... } | |
} | |
menu.setOnClickListener(listener) | |
menu.setOnExpandListener(listener) |
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 Person private constructor(val name: String, val age: Int) { | |
companion object Factory { | |
fun create(name: String, age: Int) = Person(name, age) | |
} | |
} | |
// members of the companion object can be called using the class name as qualifier | |
val bob = Person.create("Bob", 21) |
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
// object class as a singleton | |
object Log { | |
val prop = 42 | |
fun debug(msg: String) { ... } | |
fun info(msg: String) { ... } | |
fun warn(msg: String) { ... } | |
fun error(msg: String) { ... } | |
} | |
// object classes can inherit from classes and interfaces |
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 A { | |
// nested class (equivalent to static in Java) | |
class B { | |
// A can't see private members of nested classes | |
private val invisible = 42 | |
} | |
// inner class | |
inner class C { | |
// A can't see private members of inner classes |
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 Point(val x: Double, val y: Double) { | |
// addition operator | |
operator fun plus(other: Point): Point { | |
return Point(x + other.x, y + other.y) | |
} | |
// overload using Double | |
operator fun plus(value: Double): Point { | |
return Point(x + value, y + value) | |
} |
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 View { | |
fun click() | |
fun toggle() | |
} | |
class ViewImpl: View { | |
override fun click() { println("click!") } | |
override fun toggle() { println("toggle!") } | |
} |
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 A { | |
val a: Int // abstract | |
val b: Int // abstract | |
fun foo() // abstract | |
fun bar() { | |
// optionnal body | |
} | |
} | |
interface B { ... } |
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
// final public class (default) | |
class A | |
// final private class (only visible in the file) | |
private class B | |
// final internal class (only visible in the module) | |
internal class C | |
// abstract public class |
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 DeepThought { | |
val answer: String by lazy { | |
sleep(7_500_000, ChronoUnit.YEARS) | |
"42" | |
} | |
} |
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 Presenter { | |
lateinit var view: View | |
fun setup(_view: View) { | |
// ensure the lateinit var hasn't been initialized | |
if(this::view.isInitialized) throw IllegalStateException() | |
view = _view | |
} | |
} |