将java转换为scala

时间:2015-02-18 01:14:02

标签: scala

我有一个Java的工作代码,但我想在scala中转换相同的代码。这就是我的Java代码:

public class ScannerError extends RuntimeException {
  public ScannerError(String expected, Token look) {
    super(look.fileName + ":" + look.lineNumber + 
          " Expected: " + expected + 
          " Found: " + look);
  }
}

我在scala中将上面的代码转换为:

import scala.collection.JavaConversions._
object ScannerError(expected: String, look: Token) extends RuntimeException(look.fileName + ":" + look.lineNumber + " Expected: " + 
expected + " Found: " + look)

scala代码在expected:时出错。错误消息是"特征或对象可能没有参数"。

1 个答案:

答案 0 :(得分:0)

object是单例模式的scala编码,单例不按定义获取参数。 改为使用class

class ScannerError(expected: String, look: Token)
  extends RuntimeException(s"${look.fileName}: ${look.lineNumber} Expected: $expected Found: $look")