0

这里的问题:我有包含已编译的 scala 代码的 jar。在罐子里我看到

Step.class Step$class.class

两者都是合法的java类。

但是,当我尝试在 Intellij Idea java 项目中使用 Step$class 时,它会显示:“无法解析符号 Step$class”。但是代码是用 maven 编译的,所以我认为问题出在 IDE 中。

4

2 回答 2

1

Scala traits can contain implementations, unlike Java interfaces. scalac compiles this into an interface and an implementation class:

  ~/code/scratch/20120505 cat trait.scala 
package test

trait T {
    def a = println("foo")
}
  ~/code/scratch/20120505 scalac210 -d . trait.scala 

  ~/code/scratch/20120505 javap -classpath . test/T
Compiled from "trait.scala"
public interface test.T extends scala.ScalaObject{
    public abstract void a();
}

  ~/code/scratch/20120505 javap -classpath . test/T\$class
Compiled from "trait.scala"
public abstract class test.T$class extends java.lang.Object{
    public static void a(test.T);
    public static void $init$(test.T);
}

The IntelliJ Scala plugin does not expose the implementation class to Java code. It's not really a good idea to use this directly, as you are relying on an implementation detail of the Scala compiler.

You should invoke the corresponding methods on a subclass of the trait. You'll have to write that in Scala, though.

More info: How are Scala traits compiled into Java bytecode?

于 2012-05-05T09:47:09.390 回答
0

Ups,愚蠢的我,我刚刚使所有缓存和重新导入的 jar 无效,现在它得到了解决。

于 2012-05-05T09:37:29.690 回答