我正在构建一个 Matrix 类并希望能够存储 Number
s 在二维数组中。
var data: Array<Array<Number>> = Array(width, {Array(height, {0})})
这不起作用,因为 Array<Number>
和Array<Int>
是不变的。
我可以通过使用 Array<Array<out Number>>
使其工作,但矩阵是不可变的,我不希望这样......
选角{0 as Int}
使编译器错误消失,但这似乎不是一个好主意。我也想做加法之类的事情,我注意到不可能添加 Number
s:
var n: Number = 1
n + 1 // does not work
那么我该如何解决这个问题呢?为什么我不能添加两个 Number
是吗?
最佳答案
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不是特定的已知类。虽然可能有更好的解决方案,但不知 Prop 体类型。扩展函数主要只是基于类型的自动转换,但不能有单个变量,因为它需要定义为一个数字才能接受所有类型,并且因为有两个变量都需要正确的转换根据传递的类型,它最终会有点困惑。
关于arrays - 数组<编号> : get and set Int values without casting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316072/