android - kotlin中times方法的作用是什么?
问题描述
我是编程世界的新手,我正在研究运算符重载,我希望您向我解释一下 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。
推荐阅读
- c++ - 如何从文件 .txt 设置动态数组大小
- json - Why is JSON_QUERY sending back a null value?
- angularjs - AngularJS - How to store JWT token in $localStorage
- angular - Angular .pipe and .subscribe undefined when selecting from ngrx store in unit tests
- servicebus - 如何使用 webjob sdk 的内置功能发送服务总线消息
- javascript - 使用箭头函数渲染道具、Apollo 和 JSX 道具
- mysql - Adventureworks 和 MSSQL 工作台
- java - 创建配置 MAVEN、CXF 和 spring 的示例应用程序
- c# - 实现接口(如果存在)
- vb.net - 在vb中的每一行datagridview中检查值