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

Property and Backing Field

Property and Backing Field

CA.kt #2

Moyuru Aizawa

July 19, 2017
Tweet

More Decks by Moyuru Aizawa

Other Decks in Programming

Transcript

  1. 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. 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. 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. 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. class Person { val firstName: String val lastName: String val

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

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

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

    fullName get() = "$firstName $lastName" … } person.fullName Declare custom accessors
  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; } @NotNull public final String getFullName() { return this.firstName + " " + this.lastName; } } Declare custom accessors
  10. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. ‣ 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