3

我正在构建一个 Matrix 类,并希望能够将Numbers 存储在二维数组中。

var data: Array<Array<Number>> = Array(width, {Array(height, {0})})

这不起作用,因为Array<Number>并且Array<Int>是不变的。我可以通过使用使其工作Array<Array<out Number>>,但 Matrix 将是不可变的,我不希望这样......

强制转换{0 as Int}使编译器错误消失,但这似乎不是一个好主意。我也想做加法之类的事情,我注意到不可能添加Numbers:

var n: Number = 1
n + 1 // does not work

那么我该如何解决这个问题呢?为什么我不能添加两个Numbers?

4

1 回答 1

2

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 都需要正确转换根据传递的类型,它最终会有点混乱。

于 2018-05-13T13:18:48.850 回答