无法确定如何从scala覆盖java基类

时间:2015-06-22 05:57:39

标签: scala

以下是java基类中的方法

public interface BenchmarkDriver {
    public boolean test(Map<Object, Object> ctx) throws Exception;

这是Scala类:

class SparkSqlQueryBenchmark extends BenchmarkDriver

我的(不正确)尝试覆盖java接口方法:

@throws(classOf[java.lang.Exception])
override def test(ctx: java.util.Map[java.lang.Object, java.lang.Object]) : java.lang.Boolean =  {

以下是编译器错误:

[ERROR]  SparkAbstractBenchmark.scala:24: error: class SparkAbstractBenchmark needs to be abstract, since method test in trait BenchmarkDriver 
of type (x$1: java.util.Map[Object,Object])Boolean is not defined
SparkSqlQueryBenchmark.scala:30: error: overriding method test in trait BenchmarkDriver
 of type (x$1: java.util.Map[Object,Object])Boolean;
    [ERROR]  method test has incompatible type
    [ERROR]    override def test(ctx: java.util.Map[java.lang.Object, java.lang.Object])
 : java.lang.Boolean =  {
    [ERROR]

那么需要在scala覆盖方面修复什么?

更新在Marth建议更改为布尔

之后
@throws(classOf[java.lang.Exception])
override def test(ctx: java.util.Map[java.lang.Object, java.lang.Object]) : Boolean =  {

错误看起来很相似,但无论如何它都是

[ERROR]  SparkAbstractBenchmark.scala:24: error: class SparkAbstractBenchmark needs
 to be abstract, since method test in trait BenchmarkDriver of type (x$1: 
java.util.Map[Object,Object])Boolean is not defined
[ERROR] class SparkAbstractBenchmark(cacheName: String) extends BenchmarkDriverAdapter {
[ERROR]       ^
[ERROR] one error found

1 个答案:

答案 0 :(得分:6)

问题是Scala的Boolean不等同于Java的Boolean,而是等同于boolean

更改Scala方法以返回Boolean(不是java.lang.Boolean),或更改Java界面以返回Boolean

基本上:

  Java  |      Scala
----------------------------
Boolean | java.lang.Boolean
boolean | Boolean