我想将以下代码从
Java翻译成Scala:
Foo foo = new Foo() { private static final long serialVersionUID = 12345L; }
Foo类是一个抽象类.
Scala中的等效代码如何?
解决方法
有一个
Scala annotation for adding the ID.但似乎,你不能将此解决方案应用于匿名内部类.但是,根据
Scala FAQ:
In Scala private values that evaluate
to a constant known at compile-time
are turned into private static final
Java variables. This undocumented
feature should do the trick for you.
Just check out the implementation of
lists in Scala (see e.g.
src/scala/List.java). Both classes ::
and Nil have a field serialVersionUID
of the following form:
private val serialVersionUID = numeric literal;
object Ser extends Application { trait Foo { def xy: Int } val x = new Foo with java.io.Serializable { def xy = 2; private val serialVersionUID = 1L } }
用2.8.1编译器编译好.我还没有测试过,尽管结果类的串行版本实际上是提供的.