Java中的继承与对象序列化
在序列化中,当引入继承时,根据超类和子类,定义了某些情况,这使得对每种情况下序列化的理解更加简单。应该遵循的基本规则如下所示。
1. 超类实现了Serializable接口,而子类没有。
在这种情况下,即使子类没有实现Serializable接口,子类的对象也会在超类序列化时默认被序列化。
示例
public class TestSerialization {
public static void main(String[] args) throws IOException, ClassNotFoundException {
B obj = new B();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
B obj1 = (B)ois.readObject();
System.out.println(obj1.i + " " + obj1.j);
}
}
class A implements Serializable {
int i = 10;
}
class B extends A {
int j =20;
}输出
value of i is : 10 & value of j is : 20
2. 超类没有实现Serializable接口,而子类实现了。
在这种情况下,继承自子类的超类实例变量不会被序列化,并且在子类序列化期间会丢失其分配的值。此外,JVM在子类序列化期间会将默认初始化值重新分配给这些超类的实例变量。在此场景中需要注意的另一点是,超类必须具有默认的无参数构造函数,因为JVM在反序列化期间会访问超类。如果此构造函数不存在,则会遇到编译时异常。
示例
public class TestSerialization {
public static void main(String[] args) throws IOException,ClassNotFoundException {
B obj = new B(10,20);
FileOutputStream fos = new FileOutputStream("abcd.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
FileInputStream fis = new FileInputStream("abcd.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
B obj1 = (B) ois.readObject();
System.out.println("value of i is : " +obj1.i + " & value of j is : " + obj1.j);
}
}
class A {
int i;
A() {
System.out.println("default constructor called of parent class.");
}
A(int i) {
this.i=i;
}
}
class B extends A implements Serializable {
int j;
B(int i , int j) {
super(i);
this.j=j;
}
}输出1
存在默认构造函数时。
default constructor called of parent class. value of i is : 0 & value of j is : 20
输出2
不存在默认构造函数时。
Exception in thread "main" java.io.InvalidClassException: B; B; no valid constructor
3. 需要序列化超类但不序列化子类(自定义序列化)。
为了防止子类被序列化,我们需要实现writeObject()和readObject()方法,这些方法在序列化和反序列化期间由JVM执行,并且需要从这些方法中抛出NotSerializableException异常。我们也可以在这些方法中提供自定义逻辑,这些逻辑将在序列化/反序列化期间执行。
示例
public class TestSerialization {
public static void main(String[] args) throws IOException, ClassNotFoundException {
B obj = new B();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
B obj1 = (B)ois.readObject();
System.out.println("value of i is : " + obj1.i + " & value of j is : " + obj1.j);
}
}
class A implements Serializable {
int i = 10;
}
class B extends A {
int j =20;
}
// implementing writeObject method,
private void writeObject(ObjectOutputStream out) throws IOException {
throw new NotSerializableException();
}
// implementing readObject method,
private void readObject(ObjectInputStream in) throws IOException {
throw new NotSerializableException();
}输出
Exception in thread "main" java.io.NotSerializableException at B.writeObject(A.java:20)
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP