Foo.java
public class Foo{
public int i = 0;
}
巴尔斯卡拉
class Bar() extends Foo with Serializable{
i = 1
}
通过 Josh Seureth 进行序列化https://stackoverflow.com/a/3442574/390708
import java.io._
class Serialization{
def write(x : AnyRef) {
val output = new ObjectOutputStream(new FileOutputStream("test.obj"))
output.writeObject(x)
output.close()
}
def read[A] = {
val input = new ObjectInputStream(new FileInputStream("test.obj"))
val obj = input.readObject()
input.close()
obj.asInstanceOf[A]
}
}
REPL 会话,bar 在序列化之前为 1,但在序列化之后为 0。
scala -cp .
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_03).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val bar = new Bar
bar: Bar = Bar@2d2ab673
scala> bar.i
res0: Int = 1
scala> :load Serialization.scala
Loading Serialization.scala...
import java.io._
defined class Serialization
scala> val serialization = new Serialization
serial: Serialization = Serialization@41a45f89
scala> serialization.write(bar)
scala> val bars = serialization.read[Bar]
bars: Bar = Bar@5a9948fd
scala> bars.i
res3: Int = 0
那么,在这种情况下,为什么 bar.i 不是 1 呢?