Kotlin中的运算符重载以及此代码如何工作? [英] Operator overloading in Kotlin and how does this code work?

查看:46
本文介绍了Kotlin中的运算符重载以及此代码如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从教程中阅读以下代码,但我不太了解.
基本上,它尝试使用运算符重载,以便以下代码起作用:

I am reading the following code from a tutorial but I don't really get it.
Basically it tries to use operator overloading so that the following code works:

return today + YEAR * 2 + WEEK * 3 + DAY * 5  

我的理解:
这部分:

What I understand:
This part:

operator fun MyDate.plus(timeInterval: TimeInterval): MyDate {   
    return addTimeIntervals(timeInterval, 1)  
} 

使用 timeInterval 增强类 MyDate 以支持 + ,这样就可以在 myDate + YEAR 上使用例子.

Enhances the class MyDate to support the + with a timeInterval so this would work myDate + YEAR for example.

此部分:

operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval) 
    = addTimeIntervals(timeIntervals.timeInterval, timeIntervals.number)  

使用 RepeatedInterval

这部分只是声明一个带有2个成员变量 timeInterval number

This part just declares an empty class with 2 member variables timeInterval and number

class RepeatedTimeInterval(val timeInterval: TimeInterval, val number: Int)  

我不明白的是乘法实际上是如何发生的因为 RepeatedInterval 只是一个空类.
有人可以帮我理解这一点吗?

What I don't understand is how the multiplication is actually happening since RepeatedInterval is just an empty class.
Could someone please help my understand this?

import TimeInterval.*  

data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int)  

enum class TimeInterval { DAY, WEEK, YEAR }  

operator fun MyDate.plus(timeInterval: TimeInterval): MyDate {  
    return addTimeIntervals(timeInterval, 1)  
}  

class RepeatedTimeInterval(val timeInterval: TimeInterval, val number: Int)    
operator fun TimeInterval.times(number: Int) = RepeatedTimeInterval(this,   number)  



operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval)   
    = addTimeIntervals(timeIntervals.timeInterval, timeIntervals.number)  




fun task1(today: MyDate): MyDate {    
    return today + YEAR + WEEK  
}  

fun task2(today: MyDate): MyDate {   
    return today + YEAR * 2 + WEEK * 3 + DAY * 5   
}  

这也是本教程的一部分:

Also this is part of the tutorial:

import java.util.Calendar  

fun MyDate.addTimeIntervals(timeInterval: TimeInterval, number: Int): MyDate {   
    val c = Calendar.getInstance()  
    c.set(year, month, dayOfMonth)  
    when (timeInterval) {  
        TimeInterval.DAY -> c.add(Calendar.DAY_OF_MONTH, number)  
        TimeInterval.WEEK -> c.add(Calendar.WEEK_OF_MONTH, number)  
        TimeInterval.YEAR -> c.add(Calendar.YEAR, number)  
    }  
    return MyDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH),   c.get(Calendar.DATE))  
}  

推荐答案

YEAR * 2 TimeInterval * Int .编译器认为它不是内置组合,因此会在 TimeInterval 上寻找标记为 operator 的方法 times ,该方法接受 Int (例如 TimeInterval.times(Int) TimeInterval.times(Any)).此方法可以是 TimeInterval 的成员或扩展名;完全没有理由让它成为 RepeatedTimeInterval 的成员.

YEAR * 2 is TimeInterval * Int. The compiler sees it isn't a built-in combination, so it looks for a method times marked as operator on TimeInterval which accepts Int (so e.g. TimeInterval.times(Int) or TimeInterval.times(Any)). This method can be a member of TimeInterval or an extension; there's absolutely no reason for it to be a member of RepeatedTimeInterval.

实际上, RepeatedTimeInterval 根本没有解决 YEAR * 2 的任何部分,只是碰巧是返回类型.然后, today + YEAR * 2 MyDate + RepeatedTimeInterval ,并且应用相同的规则来选择运算符myDate.plus(timeIntervals:RepeatedTimeInterval)(而不是用于 today + YEAR operator fun MyDate.plus(timeInterval:TimeInterval).

In fact, RepeatedTimeInterval doesn't have any part in resolving YEAR * 2 at all, it just happens to be the return type. Then today + YEAR * 2 is MyDate + RepeatedTimeInterval and the same rule is applied to pick operator fun MyDate.plus(timeIntervals: RepeatedTimeInterval) (and not operator fun MyDate.plus(timeInterval: TimeInterval) which is used for today + YEAR).

请注意,使用此代码,例如 YEAR * 2 * 2 ;那个需要 RepeatedTimeInterval.times(Int),它又可以是成员或扩展名.

Note that with this code it isn't legal to have e.g. YEAR * 2 * 2; that would require RepeatedTimeInterval.times(Int) which again could be a member or an extension.

这篇关于Kotlin中的运算符重载以及此代码如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆