1. Read process of custom variables

Let’s first look at the full syntax for defining variables in Kotlin:

var <propertyName>[:PropertyType][= <property_initializer>]
    [<getter>]
    [<setter>]
Copy the code

The type, initial value, getter, and setter are all optional.

Val isGood get() = 100 > 90; // Customize the getter by xiaojin on 7/25/21 11:50am val isGood get() = 100 > 90 Score = 3 set(value) {by xiaojin on 7/25/21 11:52am field = value.div(10)}Copy the code

A custom getter is defined, so it is called every time the property is accessed. A custom setter is defined, so it is accessed every time a value is assigned to a property. By convention, the parameter name of the setter is value.

2. Backstage fields

Kotlin automatically provides a backroom field when a property requires it, which can be accessed using a filed identifier. It can only be used in the accessor of a property.