Number 是一个抽象类,并没有为加法定义任何东西。而且由于没有定义添加数字的方法,所以你不能这样做numberInstane + otherNumberInstance
。但是,您可以为它创建一个运算符函数:
infix operator fun Number.plus(other: Number) : Number{
return when (this) {
is Double -> this + other.toDouble()
is Int -> this + other.toInt()
is Long -> this + other.toLong()
is Float -> this + other.toFloat()
is Short -> this + other.toShort()
is Byte -> this + other.toByte()
else -> 0
}
}
请注意,这仅适用于加法。其余函数将遵循相同的模式,但替换运算符(此处为+
)和函数名称(此处为plus
)。
正如 mer msrd0 的评论,上面将导致 1 + 1.5 为 2,因为它向下舍入。Kotlin 支持相互添加数字类型,最终得到了这个有点可怕的解决方案:
infix operator fun Number.plus(other: Number) : Number{
when {
this is Double -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
this is Int -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
this is Long -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
this is Float -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
this is Short -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
this is Byte -> {
return when(other){
is Double -> this + other
is Int -> this + other
is Long -> this + other
is Float -> this + other
is Short -> this + other
is Byte -> this + other
else -> 0
}
}
else -> return 0
}
}
嵌套的 when 语句有助于自动转换值,这是必要的,因为 Number 不是特定的已知类。虽然可能有更好的解决方案,但不知道具体类型。扩展函数主要是根据类型自动转换,但不能有单个变量,因为它需要定义为 Number 才能接受所有类型,并且因为有两个 var 都需要正确转换根据传递的类型,它最终会有点混乱。