作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看起来 scala 将伴生对象中的方法编译为静态方法,这使得从 Java 代码中调用它们更容易一些。例如,您可以编写 CompanionObject.method() 而不是 CompanionObject$.MODULE$.method()。但是,有时看似无关的代码更改会破坏这种行为。我想出了这个例子来说明问题
$ cat TestCompanion.scala
class TestCompanion
object TestCompanion {
def init2 {}
}
@SerialVersionUID(1L)
class TestCompanion2
object TestCompanion2 {
def init2 {}
}
$ scalac -version
Scala compiler version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL
$ scalac TestCompanion.scala
$ javap TestCompanion
Compiled from "TestCompanion.scala"
public class TestCompanion extends java.lang.Object implements scala.ScalaObject{
public static final void init2();
public TestCompanion();
}
$ javap TestCompanion2
Compiled from "TestCompanion.scala"
public class TestCompanion2 extends java.lang.Object implements scala.ScalaObject{
public static final long serialVersionUID;
public static {};
public TestCompanion2();
}
最佳答案
这是一个已知错误:Static forwarders are missing after adding @SerialVersionUID ,由 Josh Cough 提出。从错误描述来看:
In the following code, adding @SerialVersionUID to the class results in the static forwarders missing from the byte code.
object WithoutUID {
val instance = new WithoutUID
}
class WithoutUID extends scala.Serializable
object WithUID {
val instance = new WithUID
}
@SerialVersionUID(0)
class WithUID extends scala.Serializable
Here is the relevant decompiled byte code:
public class WithoutUID implements Serializable, ScalaObject {
public static final WithoutUID instance(){
return WithoutUID.MODULE$.instance();
}
}
public class WithUID implements Serializable, ScalaObject {
public static final long serialVersionUID = 0L;
}
关于scala - 伴生对象中的方法编译为 Scala 中的静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7923852/
我是一名优秀的程序员,十分优秀!