重写case类构造函数

时间:2014-12-03 22:33:58

标签: scala

尝试重载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)
      }

1 个答案:

答案 0 :(得分:3)

重载的构造函数不存在

isVisitedadjacentNodes。看起来您打算使用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)
}