使用替代方法重载方法值

时间:2014-03-14 08:21:14

标签: scala overloading

我有以下内容:

def calc(dir: File): Option[Group] = {
 calcModel(dir) match {
  case Some(model: Model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)
  case None =>               None
 }
}

def calcUI(model: Model, availableWidth: Double, availableHeight: Double, maxLevel: Int): Option[Group] = {
}

def calcUI(model: Model, posX: Double, posY: Double, availableWidth: Double, availableHeight: Double, horizontally: Boolean, startLevel: Int, maxLevel: Int): Option[Group] = {
}

protected def calcUI(node: Node, posX: Double, posY: Double, availableWidth: Double, availableHeight: Double, horizontally: Boolean, level: Int, maxLevel: Int): Group = {
}

def calcModel(dir: File): Option[Model] = {
}

(注:模型不是从Node派生的)

并获取以下编译器错误消息:

Error:(88, 27) overloaded method value calcUI with alternatives:
  (node: org.rob.spaceview.modell.Node,posX: Double,posY: Double,availableWidth: Double,availableHeight: Double,horizontally: Boolean,level: Int,maxLevel: Int)javafx.scene.Group <and>
  (model: org.rob.spaceview.modell.Model,posX: Double,posY: Double,availableWidth: Double,availableHeight: Double,horizontally: Boolean,startLevel: Int,maxLevel: Int)Option[javafx.scene.Group] <and>
  (model: org.rob.spaceview.modell.Model,availableWidth: Double,availableHeight: Double,maxLevel: Int)Option[javafx.scene.Group]
 cannot be applied to (org.rob.spaceview.modell.Model, Double, Double, Int, Int)
      case Some(model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)
                      ^

我不明白。所有calcUI函数都有不同的参数。

我知道这个错误,大部分时间我都知道并修复它。但在这里,没有任何线索。

希望有人能够吸引我。感谢。

1 个答案:

答案 0 :(得分:10)

实际上,调用中的给定参数

case Some(model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)

不对应任何已定义的calcUI方法。例如,对于以下定义

def calcUI(model: Model, availableWidth: Double, availableHeight: Double, maxLevel: Int): Option[Group]

你还有一个不需要的论点。