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

Destructuring methods

By convention, any instance of a class that has a series of methods named component1(), component2() and so on can be used in a destructuring declaration.

Kotlin will generate these methods for any data class:

val (prod: BakeryGood, price: Double, qty: Int) = mySecondItem

The prod value is initialized with the return of component1(), price with the return of component2() , and so on. Although the preceding example use explicit types, those aren't needed:

val (prod, price, qty) = mySecondItem

In some circumstances, not all values are needed. All unused values can be replaced by (_):

val (prod, _, qty) = mySecondItem
主站蜘蛛池模板: 衡南县| 龙里县| 雷州市| 拉孜县| 旬阳县| 安岳县| 红原县| 济宁市| 东明县| 郯城县| 大余县| 沾化县| 宣汉县| 秭归县| 绩溪县| 四会市| 明溪县| 大石桥市| 凌海市| 翼城县| 新干县| 西充县| 广丰县| 玛沁县| 建阳市| 卓尼县| 陈巴尔虎旗| 旅游| 安阳县| 会泽县| 木兰县| 韶山市| 平安县| 乌鲁木齐市| 黑龙江省| 慈溪市| 海晏县| 张家川| 义马市| 申扎县| 瓦房店市|