序列化和反序列化(三)——父类的序列化

1、一个父类实现序列化,子类自动实现序列化;

      示例:

package com.jd.vo;

import java.io.Serializable;

public class Father implements Serializable{

	private static final long serialVersionUID = 6614009233928674796L;
	public int age;
}
package com.jd.vo;

public class Son extends Father {

	private static final long serialVersionUID = -7604257792005206061L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.jd.test;

import java.io.*;

import com.jd.vo.Son;

public class Test {

	/**
	 * 序列化
	 * 
	 * @author GaoHuanjie
	 */
	public static void serialize() {
		Son son = new Son();
		son.age = 12;
		son.setName("Tom");
		System.out.println(son);
		ObjectOutput objectOutput = null;
		try {
			objectOutput = new ObjectOutputStream(new FileOutputStream("D:\\son.ser"));
			objectOutput.writeObject(son);
			objectOutput.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutput != null) {
				try {
					objectOutput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 反序列化
	 * 
	 * @author GaoHuanjie
	 */
	public static void deserialize() {
		ObjectInput objectInput = null;
		try {
			objectInput = new ObjectInputStream(new FileInputStream("D:\\son.ser"));
			Son son = (Son) objectInput.readObject();
			System.out.println(son.age);//输出12 age属性是Son类从Father类继承过来的,由于父类实现Serializable接口,所以序列化子类时age属性也会被序列化。
			System.out.println(son.getName());//输出Tom 尽管Son类没有实现Serializable接口,但由于其父类实现了该接口,所以Son类中name属性依然被序列化。
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (objectInput != null) {
				try {
					objectInput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		serialize();// 序列化
		deserialize();// 反序列化
	}
}

2、子类实现序列化,父类没有实现,则子类继承父类的属性在序列化子类时不会被序列化;

      示例:

package com.jd.vo;

public class Father{

	public int age;
}
package com.jd.vo;

import java.io.Serializable;

public class Son extends Father implements Serializable {

	private static final long serialVersionUID = -7604257792005206061L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.jd.test;

import java.io.*;

import com.jd.vo.Son;

public class Test {

	/**
	 * 序列化
	 * 
	 * @author GaoHuanjie
	 */
	public static void serialize() {
		Son son = new Son();
		son.age = 12;
		son.setName("Tom");
		System.out.println(son);
		ObjectOutput objectOutput = null;
		try {
			objectOutput = new ObjectOutputStream(new FileOutputStream("D:\\son.ser"));
			objectOutput.writeObject(son);
			objectOutput.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutput != null) {
				try {
					objectOutput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 反序列化
	 * 
	 * @author GaoHuanjie
	 */
	public static void deserialize() {
		ObjectInput objectInput = null;
		try {
			objectInput = new ObjectInputStream(new FileInputStream("D:\\son.ser"));
			Son son = (Son) objectInput.readObject();
			System.out.println(son.age);//age属性是Son类从Father类继承过来的,但由于父类没有实现Serializable接口,所以序列化子类时age属性并不会被序列化。
			System.out.println(son.getName());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (objectInput != null) {
				try {
					objectInput.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		serialize();// 序列化
		deserialize();// 反序列化
	}
}

3、子类实现序列化,父类没有实现,则父类必须提供无参构造方法;

      例子:

package com.jd.vo;

public class Father{

	public int age;
	
	public Father() {//该无参构造方法不可省略,否则出现“java.io.InvalidClassException: com.jd.vo.
Son; no valid constructor”异常
		
	}
	public Father(int age) {
		this.age = age;
	}
}
package com.jd.vo;

import java.io.Serializable;

public class Son extends Father  implements Serializable{

	private static final long serialVersionUID = -7604257792005206061L;
	
	public Son(int age) {
		super(age);
		this.age = age;
	}
	
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
package com.jd.test;

import java.io.*;

import com.jd.vo.Son;

public class Test {

	public static void main(String[] args) throws Exception {
		try {
			// 序列化
			ObjectOutput objectOutput = new ObjectOutputStream(new FileOutputStream("D:\\son.ser"));
			Son son = new Son(12);
			son.setName("Tom");
			System.out.println(son);
			objectOutput.writeObject(son);
			objectOutput.flush();
			objectOutput.close();

			// 反序列化
			ObjectInput objectInput = new ObjectInputStream(new FileInputStream("D:\\son.ser"));
			son = (Son) objectInput.readObject();
			objectInput.close();
			System.out.println(son.age);
			System.out.println(son.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

发布了327 篇原创文章 · 获赞 571 · 访问量 164万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览