Property and Backing Field

Property and Backing Field

CA.kt #2

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

July 19, 2017
Tweet

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 12.

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

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  6. 13.

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

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  7. 14.

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

    fullName get() = "$firstName $lastName" … } Declare custom accessors
  8. 15.

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

    fullName get() = "$firstName $lastName" … } person.fullName Declare custom accessors
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 40.