Scala 将整数映射到对象时出现错误“;无法解析重载构造函数“;
我正在尝试将整数映射到Scala中的对象。我是Scala新手,所以我面临这个错误“无法解析重载的构造函数项”。请帮助解决此错误Scala 将整数映射到对象时出现错误“;无法解析重载构造函数“;,scala,oop,Scala,Oop,我正在尝试将整数映射到Scala中的对象。我是Scala新手,所以我面临这个错误“无法解析重载的构造函数项”。请帮助解决此错误 object Main extends App { override def main(args: Array[String]): Unit = { val listMap1: ListMap[Int, Item] = ListMap(1 -> new Item("mobile", 25000, 1) ) } } 我在这一部分遇到的错误是: n
object Main extends App {
override def main(args: Array[String]): Unit = {
val listMap1: ListMap[Int, Item] = ListMap(1 -> new Item("mobile", 25000, 1) )
}
}
我在这一部分遇到的错误是:
new Item("mobile", 25000, 1)
这是我的班级项目:
class Item (name: String, price: Int, id: Int){
def this(name: String, price: Int, id: Int) = {
this(name,price,id);
}
}
在Scala中,描述类时放在参数中的东西是构造函数。然后用
def this…
重载它,由于它具有相同的签名,编译器无法确定使用哪个签名
注意,对于这样的情况,case类
提供了很多很好的语法糖分。最好的办法是做类似的事情
case class Item(name: String, price: Int, id: Int)
val map: Map[Int, Item] = Map[Int, Item](1 -> Item("mobile", 25000, 1))
你有两条本质上相同的收缩肌。编译器无法确定调用哪一个。-为什么有第二个构造函数?它什么也没做PS:如果您只想拥有一个只表示数据的普通对象,那么应该改用case类。它将免费为您提供许多有用的东西。好的,谢谢@LuisMiguelMejíaSuárez我删除了
def this()
,现在错误消失了。