尝试重载case类的构造函数参数时:
case class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
def this(name : String) = this(isVisited , adjacentNodes , name)
}
收到此错误:not found: value isVisited
如果这不能按照接受的答案中解释的那样工作:
Overload constructor for Scala's Case Classes?
但是,这可行,但不使用案例类:
class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
def adjacentNodes(): scala.collection.mutable.MutableList[Node] = { adjacentNodes }
def name(): String = { name }
}
object Node {
def apply(name: String): Node = new Node(false, scala.collection.mutable.MutableList[Node](), name)
}
答案 0 :(得分:3)
isVisited
和adjacentNodes
。看起来您打算使用false
和空列表,如果没有提供它们:
case class Node(var isVisited: Boolean, adjacentNodes: scala.collection.mutable.MutableList[Node], name: String) {
def this(name : String) = this(false, scala.collection.mutable.MutableList[Node](), name)
}