用Kotlin-koans学Kotlin【四】 iii_conventions

本节练习Kotlin中各种接口的使用

25. n25Comparison

Mydate类实现Comparable接口,式其可以通过我们在Comparable接口中定义的规则直接比较

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

DateRange类实现方法,判断某个Mydate对象是否在DateRange的范围中

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

也可以

class DateRange(override val start: MyDate, override val endInclusive: MyDate) : ClosedRange<MyDate> {
    override fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
}复制代码

参数前添加了override是因为CloseRange接口中也定义了这两个属性,所以我们实现该接口也就复写了这两个参数

27. n27RangeTo

实现rangeTo接口,该接口的功能是返回一个从当前对象到目标对象的一个集合

operator fun MyDate.rangeTo(other: MyDate): DateRange =DateRange(this,other)复制代码
28. n28ForLoop

DateRange实现Iterable接口
可以专门创建一个类,也可以用对象表达式

class DateRange(override val start: MyDate, override val endInclusive: MyDate) : ClosedRange<MyDate>, Iterable<MyDate> {
    override fun iterator(): Iterator<MyDate> = object : Iterator<MyDate> {
        var current: MyDate = start
        override fun next(): MyDate {
            val result = current
            current = current.nextDay()
            return result
        }
        override fun hasNext(): Boolean = current <= endInclusive

    }

    override fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
}复制代码
class DateRange(override val start: MyDate, override val endInclusive: MyDate) : ClosedRange<MyDate>, Iterable<MyDate> {
    override fun iterator(): Iterator<MyDate> =DateIterator(this)
    override fun contains(item: MyDate): Boolean = start <= item && item <= endInclusive
}

class DateIterator(val dateRange:DateRange) : Iterator<MyDate> {
    var current: MyDate = dateRange.start
    override fun next(): MyDate {
        val result = current
        current = current.nextDay()
        return result
    }
    override fun hasNext(): Boolean = current <= dateRange.endInclusive
}复制代码
29. n29OperatorsOverloading

重载操作符,仅+

operator fun MyDate.plus(timeInterval: TimeInterval) = addTimeIntervals(timeInterval,1)复制代码

TimeInterval重载*,调用时一定要TimeInterval在前

class RepeatedTimeInterval(val ti: TimeInterval, val n: Int=1)
operator fun TimeInterval.times(number:Int) = RepeatedTimeInterval(this,number)
operator fun MyDate.plus(timeInterval: RepeatedTimeInterval) = addTimeIntervals(timeInterval.ti,timeInterval.n)复制代码
30. n30DestructuringDeclarations

用解构声明解构对象

class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) {
    operator fun component1(): Int =year
    operator fun component2(): Int =month
    operator fun component3(): Int =dayOfMonth
}复制代码
31. n31Invoke

统计调用次数,这里使用了闭包

class Invokable{
    var numberOfInvocations: Int = 0
        private set
    operator fun invoke():Invokable  {
        numberOfInvocations++
        return this
    }
}复制代码

转载于:https://juejin.im/post/59e57bcc51882578b956a2ab

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值