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 TestCreator implements Serializable{
public String name;
public TestCreator(String s){
this.name=s;
}
// static TestCreator(){
//
// }
public static void main(String args[]){
try {TestClass t=new TestClass("123");
ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("test.out"));
out.writeObject(t);
out.close();
ObjectInputStream in =new ObjectInputStream(new FileInputStream("test.out"));
TestCreator t1=(TestClass)in.readObject();
in.close();
System.out.println(t1.name);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class TestClass extends TestCreator{
public TestClass(String s) {
super(s);
this.name=this.name+"sss";
// TODO Auto-generated constructor stub
}
/**
*
*/
// private static final long serialVersionUID = 1L;
}
例子中TestClass 继承了TestCreator类,但是没有显示的声明实现serializable接口,但是仍然可以实现序列化
同时从继承的原理也可以理解这一点,父类的父类也是子类的父类
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 TestCreator implements Serializable{
public String name;
public TestCreator(String s){
this.name=s;
}
// static TestCreator(){
//
// }
public static void main(String args[]){
try {TestClass t=new TestClass("123");
ObjectOutputStream out =new ObjectOutputStream(new FileOutputStream("test.out"));
out.writeObject(t);
out.close();
ObjectInputStream in =new ObjectInputStream(new FileInputStream("test.out"));
TestCreator t1=(TestClass)in.readObject();
in.close();
System.out.println(t1.name);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class TestClass extends TestCreator{
public TestClass(String s) {
super(s);
this.name=this.name+"sss";
// TODO Auto-generated constructor stub
}
/**
*
*/
// private static final long serialVersionUID = 1L;
}
例子中TestClass 继承了TestCreator类,但是没有显示的声明实现serializable接口,但是仍然可以实现序列化
同时从继承的原理也可以理解这一点,父类的父类也是子类的父类