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

前端之家收集整理的这篇文章主要介绍了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编译器编译好.我还没有测试过,尽管结果类的串行版本实际上是提供的.

原文链接:https://www.f2er.com/java/126674.html

猜你在找的Java相关文章