【问题标题】:Overriding case class constructor覆盖案例类构造函数
【发布时间】:2015-02-01 16:17:12
【问题描述】:

当尝试重载案例类的构造函数参数时:

 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)
      }

【问题讨论】:

  • @Gábor Bakos 问题已更新,谢谢

标签: scala


【解决方案1】:

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)
}

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2011-02-09
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2011-05-01
    • 2017-04-09
    相关资源
    最近更新 更多