Upgrade to Pro — share decks privately, control downloads, hide ads and more …

3. Object-Oriented Programming in Kotlin [kotli...

3. Object-Oriented Programming in Kotlin [kotlin-workshop]

Part of https://github.com/jetBrains/kotlin-workshop.

Covers:
- Constructor syntax
- Properties
- Class modifiers (enum, data, inner, sealed)
- Objects, companion objects
- Constants
- Annotations

Svetlana Isakova

November 01, 2017
Tweet

More Decks by Svetlana Isakova

Other Decks in Programming

Transcript

  1. Full primary constructor syntax (1) class Person(name: String) { init

    { } } constructor parameter constructor body
  2. Full primary constructor syntax (2) class Person(name: String) { val

    name: String init { this.name = name } } constructor parameter constructor body
  3. val/var on parameter creates a property class Person(name: String) {

    val name: String init { this.name = name } } class Person(val name: String) is the same as:
  4. Secondary constructor class Rectangle(val height: Int, val width: Int) {

    constructor(side: Int) : this(side, side) { … } } secondary constructor primary constructor
  5. property = field + accessor(s) read-only property = field +

    getter mutable property = field + getter + setter Property ( val / var )
  6. 
 class Contact( val name: String, var address: String )

    contact.getAddress(); contact.setAddress("..."); contact.address contact.address = "..."
  7. property = field? + accessor(s) read-only property = field? +

    getter mutable property = field? + getter + setter Property ( val / var )
  8. class Rectangle(val height: Int, val width: Int) { val isSquare:

    Boolean get() { return height == width } } Backing field might be absent
  9. class StateLogger { var state = false set(value) { println("state

    has changed") field = value } } You can use field inside accessors (only)
  10. Enum class import Color.* fun getDescription(color: Color) = when (color)

    { BLUE -> "cold" ORANGE -> "mild" RED -> "hot" } enum class Color { BLUE, ORANGE, RED } represents enumeration
  11. Data modifier data class Contact(val name: String, val address: String)

    generates useful methods: equals, hashCode, copy, toString, and some others contact.copy(address = "new address")
  12. Equals & reference equality val set1 = setOf(1, 2, 3)

    val set2 = setOf(1, 2, 3) set1 == set2 set1 === set2 checks reference equality calls equals true false
  13. Sealed modifier sealed class Expr class Num(val value: Int) :

    Expr() class Sum(val left: Expr, val right: Expr) : Expr() restricts class hierarchy: all subclasses must be located in the same file fun eval(e: Expr): Int = when (e) { is Num -> e.value is Sum -> eval(e.left) + eval(e.right) }
  14. Inner modifier adds reference to the outer class class A

    { class B inner class C { …this@A… } }
  15. interface Repository { fun getById(id: Int): Customer fun getAll(): List<Customer>

    } interface Logger { fun logAll() } class Controller( repository: Repository, logger: Logger ) : Repository by repository, Logger by logger fun use(controller: Controller) { controller.logAll() } Class delegation
  16. Object = singleton public class JSingleton { public final static

    JSingleton INSTANCE = new JSingleton(); private JSingleton() { } public void foo() {} } object KSingleton { fun foo() {} }
  17. Using Kotlin object from Java public class UsingKotlinObjectFromJava { public

    static void main(String[] args) { JSingleton.INSTANCE.foo(); KSingleton.INSTANCE.foo(); } }
  18. companion object class A { companion object { fun foo()

    = 1 } } fun main(args: Array<String>) { A.foo() } special object inside a class
  19. companion object can implement an interface interface Factory<T> { fun

    create(): T } class A { private constructor() companion object : Factory<A> { override fun create(): A { return A() } }
  20. No static keyword Declare “static” members: - at the top-level

    - inside objects - inside companion objects
  21. @JvmStatic class C {
 companion object {
 @JvmStatic fun foo()

    {}
 fun bar() {}
 }
 } // Java
 C.foo();
 C.bar(); C.Companion.foo(); C.Companion.bar();
  22. @JvmStatic object Obj {
 @JvmStatic fun foo() {}
 fun bar()

    {}
 } // Java Obj.foo();
 Obj.bar();
 Obj.INSTANCE.foo(); Obj.INSTANCE.bar();
  23. @JvmField @JvmField val property = MyClass() // the same as

    // Java public static final MyClass property = new MyClass(); exposes a Kotlin property as a field in Java
  24. @JvmField makes a property static if used inside object class

    A { @JvmField val property = MyClass() } object B { @JvmField val property = MyClass() } regular field generated static field generated
  25. @JvmName // Data.kt fun foo() {
 } public final class

    DataKt {
 public static void foo() {
 }
 } @file:JvmName("FooUtils") fun foo() {
 } public final class FooUtils {
 public static final void foo() {
 }
 }
  26. @JvmName fun List<String>.filterValid(): List<String>
 fun List<Int>.filterValid(): List<Int> Error: platform declaration

    clash: declarations have the same JVM signature (filterValid(Ljava/util/List;)Ljava/util/List;)
  27. @JvmOverloads 
 fun f(a: String, b: Int = 0, c:

    String = "abc") {
 // ...
 } // Java
 void f(String a, int b, String c) { }

  28. @JvmOverloads @JvmOverloads
 fun f(a: String, b: Int = 0, c:

    String = "abc") {
 // ...
 } // Java
 void f(String a, int b, String c) { }
 void f(String a, int b) { }
 void f(String a) { }
  29. Checked exceptions // Java
 try {
 DemoKt.foo();
 }
 catch (IOException

    e) {
 // ...
 } fun foo() { throw IOException() } Error: Exception java.io.IOException is never thrown in body of corresponding try statement
  30. Checked exceptions // Java
 try {
 DemoKt.foo();
 }
 catch (IOException

    e) {
 // ...
 } @Throws(IOException::class) fun foo() { throw IOException() }