Property and Backing Field

Property and Backing Field

CA.kt #2

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

July 19, 2017
Tweet

Transcript

  1. Property and Backing Field @MoyuruAizawa

  2. Ѫᖒ๖ (Moyuru Aizawa) - Kotlin engineer at CyberAgent, Inc. -

    FRESH! lvla0805 MoyuruAizawa
  3. Property?

  4. class Person { val firstName: String val lastName: String …

    } Property
  5. class Person { val firstName: String val lastName: String …

    } Property
  6. Kotlin compiler generates Backing Fields and Accessors

  7. public final class Person { @NotNull private final String firstName;

    @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields and Accessors
  8. public final class Person { @NotNull private final String firstName;

    @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields
  9. public final class Person { @NotNull private final String firstName;

    @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Accessors
  10. public final class Person { @NotNull private final String firstName;

    @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields
  11. Declare Custom Accessors

  12. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  13. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  14. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  15. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" … } person.fullName Declare custom accessors
  16. public final class Person { @NotNull private final String firstName;

    @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } @NotNull public final String getFullName() { return this.firstName + " " + this.lastName; } } Declare custom accessors
  17. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors
  18. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors
  19. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors
  20. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors
  21. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) age = value } … } Declare custom accessors Stack Overflow
  22. class Person { val firstName: String val lastName: String val

    fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } person.age = 26 Declare custom accessors
  23. public final class Person { … private int age; …

    public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors
  24. public final class Person { … private int age; …

    public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors
  25. public final class Person { … private int age; …

    public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors
  26. var <propertyName>[: <PropertyType>] [= <property_initializer>] [<getter>] [<setter>] Declare custom accessors

  27. Properties without Backing Fields

  28. class NoBackingField { val property get() = "property" } No

    Backing Fields
  29. public final class NoBackingField { @NotNull public final String getProperty()

    { return "property"; } } No Backing Fields
  30. Property without Backing Field ≒ Function

  31. Interface can have Properties without Backing Fields

  32. interface User { val id: Long val name: String }

    data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields
  33. interface User { val id: Long val name: String }

    data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields
  34. interface User { val id: Long val name: String }

    data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields
  35. Extend a class with new Properties without Backing Fields

  36. val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get()

    = (application as CustomApplication).component Extend a class with new Properties without Backing Fields
  37. val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get()

    = (application as CustomApplication).component Extend a class with new Properties without Backing Fields
  38. val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get()

    = (application as CustomApplication).component Extend a class with new Properties without Backing Fields
  39. ‣ Kotiln generates Backing Fields and Accessors ‣ You can

    declare custom Accessors ‣ Interface can have properties without Backing Fields ‣ You can extend a class with new Properties without Backing Fields
  40. Thank you