【发布时间】:2020-05-21 20:09:06
【问题描述】:
代码目的:Class Pair可以打印出Product name和数量,Product name存放在Class Product中
class Pair<T, U>(var product: Product, var quantity: Int) {
for ( (product,quantity) in productAndQuantityList) {
println("Name: ${product.productName}")
println("Quantity: $quantity")
}
}
上述错误:(2, 9) Kotlin: Expecting member declaration 错误:(2, 57) Kotlin: 函数声明必须有名字
class ShoppingCart{
private val productAndQuantityList = mutableListOf<Pair<Product,Int> >()
...
}
open class Product(
val productName: String,
var basePrice: Double,
open val salesPrice: Double,
val description: String) {
...}
- 我可以知道如何更改我的代码吗?
- Compiler 建议 Pair 类之后,但我应该填写什么吗?
- 我应该为哪个主题工作,以避免再次出现同样的错误?
谢谢!
【问题讨论】:
-
kotlin 中已经有一个
Pair类,所以使用它,在函数中移动 for 循环代码,因为它们是可执行语句而不是声明。阅读有关方法、初始化程序等的信息。
标签: kotlin