我是编程界的新手,我正在研究运算符重载,我希望你能向我解释一下 times 方法在这个练习中实现的功能。
class Vector {
val arreglo = IntArray(5)
fun cargar() {
for (i in arreglo.indices)
arreglo[i] = (Math.random() * 11 + 1).toInt()
}
fun imprimir() {
for (elemento in arreglo)
print("$elemento ")
println()
}
operator fun times(valor: Int): Vector {
var suma = Vector()
for (i in arreglo.indices)
suma.arreglo[i] = arreglo[i] * valor
return suma
}
}
fun main(args: Array<String>) {
val vec1 = Vector()
vec1.cargar()
vec1.imprimir()
println("El producto de un vector con el número 10 es")
val vecProductoEnt = vec1 * 10
vecProductoEnt.imprimir()
}
最佳答案
函数时间重载运算符时间 (*) 并允许您编写表达式 vec1 * 10 以将 Vector 的每个元素乘以 10。
关于android - kotlin中方法时间的功能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62085708/