1

假设我有以下 Scala API,我计划从 Scala 和 Java 调用它:

sealed abstract class InterpMethod
case object InterpLinear extends InterpMethod
case object InterpNearest extends InterpMethod
case object InterpSpline extends InterpMethod

class Interpolator {
  def interp(method: InterpMethod) = method match {
    case InterpLinear => { ... }
    case InterpNearest => { ... }
    case InterpSpline => { ... }
  }
}

object Interpolator { def apply() = new Interpolator }

// Scala Usage:
import myPackage._
Interpolator.interp(InterpNearest)

// Java Usage:
import myPackage.*
Interpolator myInterp = new Interpolator()
myInterp.interp(InterpNearest)

在 Scala 中运行良好。Java抛出编译错误:InterpNearest cannot be resolved to a variable

有简单的解决方法吗?或者定义更Java友好的参数的更好方法?将参数定义为闭包会更好吗?

任何最佳实践或解决方法将不胜感激。这旨在成为更大流畅界面的一部分。我的关键标准是它需要在 Java 和 Scala 中都可用且干净。

4

1 回答 1

0

http://lampwww.epfl.ch/~michelou/scala/using-scala-from-java.html说你可以这样调用对象:

IterpNearest$.MODULE$

这不是很好。我想你应该接受@millimoose 的建议。

于 2013-05-02T14:44:05.530 回答