import java.io.*;
class Player
{
Player(){System.out.println("p");}
}
public class Cardplay extends Player implements Serializable{ Cardplay(){System.out.println("c");} public static void main(String[] args) {
Cardplay c1=new Cardplay();
try{
FileOutputStream fos=new FileOutputStream("play.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis=new FileInputStream("play.text");
ObjectInputStream is=new ObjectInputStream(fis);
c1=(Cardplay)is.readObject();
is.close();
}
catch (Exception x){} }}
-----------------------------------------------------------------------
按照书里写的反串行时会运行父类的构造函数。那么结果应该是 pcp 可我在运行时结果老是 pc. 为什么呢(我的JDK 1.6)
class Player
{
Player(){System.out.println("p");}
}
public class Cardplay extends Player implements Serializable{ Cardplay(){System.out.println("c");} public static void main(String[] args) {
Cardplay c1=new Cardplay();
try{
FileOutputStream fos=new FileOutputStream("play.txt");
ObjectOutputStream os=new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis=new FileInputStream("play.text");
ObjectInputStream is=new ObjectInputStream(fis);
c1=(Cardplay)is.readObject();
is.close();
}
catch (Exception x){} }}
-----------------------------------------------------------------------
按照书里写的反串行时会运行父类的构造函数。那么结果应该是 pcp 可我在运行时结果老是 pc. 为什么呢(我的JDK 1.6)
FileInputStream fis=new FileInputStream("play.txt");//文件后缀名写错了.
catch (Exception x){}
改成:
catch (Exception x){
x.printStackTrace();
}
//不要轻易的去除异常的处理.否则不利于找到程序的问题.