"Kotlin Koans - Conventions"의 두 판 사이의 차이

71번째 줄: 71번째 줄:
<source lang='kotlin'>
<source lang='kotlin'>
</source>
</source>
==같이 보기==
* [[Kotlin Koans]]
[[분류: Kotlin]]

2019년 4월 6일 (토) 23:58 판

# Kotlin Koans
Kotlin Koans - Introduction
Kotlin Koans - Conventions
Kotlin Koans - Collections
Kotlin Koans - Properties
Kotlin Koans - Builders
Kotlin Koans - Generics

1 Comparison

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    override fun compareTo(other: MyDate) = when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}

fun compare(date1: MyDate, date2: MyDate) = date1 < date2

2 In range

class DateRange(val start: MyDate, val endInclusive: MyDate) {
    operator fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
}

fun checkInRange(date: MyDate, first: MyDate, last: MyDate): Boolean {
    return date in DateRange(first, last)
}
MyDate.kt
data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {
    override fun compareTo(other: MyDate) = when {
        year != other.year -> year - other.year
        month != other.month -> month - other.month
        else -> dayOfMonth - other.dayOfMonth
    }
}

3 Range to

4 For loop

5 Operators overloading

6 Destructuring declarations

7 Invoke

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}