A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


[AppleScript] 纯文本查看 复制代码
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class aa implements Serializable{
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		aa it=new aa();
		ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("demo/extend115.txt"));
		ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("demo/extend115.txt"));
		//结论,调整下面的类中的的实现可序列化的位置,可以得到如下结论
		//反序列化时只显示调用未实现序列化的父类
		System.out.println("测试在fu1实现可序列化");
		System.out.println("序列化:");
		fu2 f2xlh=it.new fu2();
		objectOutputStream.writeObject(f2xlh);
		objectOutputStream.flush();
		System.out.println("反序列化");
		fu2 f2=(fu2)objectInputStream.readObject();
		System.out.println("测试在fu0实现可序列化结束");

	}
	class fu0 {
		public fu0(){
			System.out.println("fu0");
		}
	}
	class fu1 extends fu0 implements Serializable{
		public fu1(){
			System.out.println("fu1");
		}
	}
	class fu2 extends fu1{
		public fu2(){
			System.out.println("fu2");
		}
	}
	
	
}

错误提示:
测试在fu1实现可序列化
序列化:
fu0
fu1
fu2
反序列化
Exception in thread "main" java.io.InvalidClassException: fuxi.Demos.aa$fu2; no valid constructor
        at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
        at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
        at java.io.ObjectInputStream.readObject0(Unknown Source)
        at java.io.ObjectInputStream.readObject(Unknown Source)
        at fuxi.Demos.aa.main(aa.java:24)

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马