官术网_书友最值得收藏!

Data classes

Each time we create a model class, we end up writing the same boilerplate code for:

  • Constructor
  • Getter-Setter (s)
  • hashCode()
  • equals()
  • toString()

Kotlin introduced the  data keyword where the compiler automatically derives the following stuff based upon the parameters of the primary constructor:

  • Getter-Setter (s), which are not technically due to data keywords
  • equals()/hashCode() pair
  • toString()
  • componentN() functions corresponding to the properties in their order of declaration
  • copy() function

It saves a lot of boilerplate code and makes the model classes look clean and concise.

The Message class is the data structure representing the messages left by a user on the map. It holds the actual message left, the author of the message and location (coordinates) of the message on the map:

    /**
* It represents the message shown on the maps
*/
data class Message(
// The message
var content: String,
// Location of the message
var location: Point? = null,
var id: Int? = null
)
主站蜘蛛池模板: 天长市| 庆城县| 呼伦贝尔市| 苏尼特左旗| 合作市| 鄱阳县| 雷山县| 凉山| 平凉市| 嘉兴市| 徐闻县| 会东县| 博乐市| 岚皋县| 平乡县| 长乐市| 枝江市| 雅安市| 合水县| 贡觉县| 山西省| 黑山县| 虎林市| 富宁县| 昂仁县| 雷州市| 龙山县| 诏安县| 荆州市| 江西省| 延长县| 禹州市| 大丰市| 长顺县| 淳安县| 璧山县| 禹城市| 寿阳县| 屏东县| 利川市| 巴林左旗|