java – 如何将@SerialVersionUID添加到匿名类?

我想将以下代码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编译器编译好.我还没有测试过,尽管结果类的串行版本实际上是提供的.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...