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

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
)
主站蜘蛛池模板: 扶余县| 湘潭市| 泸西县| 温州市| 东城区| 旺苍县| 双江| 吉林市| 潮州市| 长顺县| 凉山| 巴林右旗| 永新县| 宜章县| 贵德县| 五峰| 林口县| 三亚市| 洛川县| 北宁市| 溆浦县| 延川县| 观塘区| 游戏| 高邑县| 万州区| 南雄市| 育儿| 鄱阳县| 新闻| 来宾市| 南康市| 定陶县| 万载县| 湄潭县| 民勤县| 康保县| 昔阳县| 花莲市| 延安市| 普宁市|