3

环境:

java:1.8.0_201
scala:2.11.8
IDE: IntelliJ IDEA 2018.3.5 (Ultimate Edition) (Build #IU-183.5912.21)
Intellij Scala plugin:V2018.3.6

我有一个像这样的类方法

class ScalaA {
  def cal(op: () => String): String = {
    op.apply()
  }
}

我想调用方法cal,我的第一次尝试是这样的

public class JavaB {

  public static void main(String[] args) {
    new ScalaA().cal(() -> "Hello");

  }
}

但是出现了编译错误:

Error:(9, 22) java: incompatible types: scala.Function0 is not a functional interface
    multiple non-overriding abstract methods found in interface scala.Function0

然后我尝试调用cal这样的方法:

public static void main(String[] args) {
    new ScalaA().cal(new Function0<String>() {
      @Override
      public String apply() {
        return "aa";
      }
    });
  }

但是还是有编译错误

错误:(9, 46) java: is not abstract 并且不会覆盖 scala.Function0 中的抽象方法 apply$mcV$sp()

似乎方法 apply$mcV$sp() 没有实现,但我没有看到名为apply$mcV$sp()inscala.Function0

trait Function0[@specialized(Specializable.Primitives) +R] extends AnyRef { self =>
  /** Apply the body of this function to the arguments.
   *  @return   the result of function application.
   */
  def apply(): R

  override def toString() = "<function0>"
}

,我应该怎么做才能解决这个问题,你能给我一些建议吗,提前谢谢!

4

0 回答 0