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

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
主站蜘蛛池模板: 桑日县| 曲阜市| 鹿泉市| 海宁市| 泾阳县| 岐山县| 海淀区| 福州市| 鱼台县| 浑源县| 定日县| 台州市| 桑日县| 黔江区| 南宫市| 交城县| 喀喇沁旗| 波密县| 高唐县| 陵水| 万安县| 会泽县| 太原市| 湘乡市| 定襄县| 胶南市| 阳江市| 泸州市| 阜城县| 九台市| 东山县| 来凤县| 资源县| 金秀| 新龙县| 十堰市| 山西省| 江阴市| 佛冈县| 灵武市| 汕尾市|