本帖最后由 love风之吻浩 于 2011-12-24 16:03 编辑
我在运行ObjectOutputStream时碰到一点问题:
下面是代码:
import java.io.*;
public class OutputStreamDemo3 {
public static void main(String []args)throws IOException,ClassNotFoundException
{
//writeobj();
readobj();
}
/*public static void writeobj()throws IOException
{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("b.txt"));
oos.writeObject(new People1("zhangsan",99));
oos.close();
}*/
public static void readobj()throws IOException,ClassNotFoundException
{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("b.txt"));
People1 p=(People1)ois.readObject();
System.out.println(p);
ois.close();
}
下面是People1 的代码:
import java.io.*;
class People1 implements Serializable{
private String name;
private int age;
People1(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return name+":"+age;
}
}
我感到比较奇怪的是,如果把这段代码中的toString()
public String toString()
{
return name+":"+age;
}
替换成别的方法名,那么从主函数中打印的就不是zhangsan:99;
而是:day21.People1@fa7e74(day21是包名);请问诸位能否解释一下?
} |