【发布时间】:2013-09-16 20:25:04
【问题描述】:
嗨,如果一个类不能实现可序列化接口,如果我们尝试序列化它,我们应该得到一个NotSerializableException. 我没有得到它。 Cat 类没有实现 Serializable,我尝试对其进行序列化。为什么编译运行正常?
import java.io.*;
class Cat{}
class MyTest{
public static void main(String a[]){
Cat c = new Cat();
try{
FileOutputStream fos = new FileOutputStream("test.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(c);
oos.close();
fos.close();
}
catch(Exception e){e.getMessage();}
try{
FileInputStream fis = new FileInputStream("test.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Cat)ois.readObject();
ois.close();
fis.close();
}
catch(Exception e){e.getMessage();}
}
}
【问题讨论】:
-
你怎么知道你没有收到错误
-
没错。使用该代码,您无法知道。
标签: java serialization