以下是看毕姥爷的视频第21天的时候的代码。运行结果是
lisi:0:kr
而视频中的结果是
lisi:0:cn
为什么我的结果不一样呢?
- import java.io.*;
- class ObjectStreamDemo
- {
- public static void main(String[] args) throws Exception
- {
- writeObj();
- readObj();
- }
- public static void readObj() throws Exception
- {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
- Person p = (Person)ois.readObject();
- System.out.println(p);
- ois.close();
- }
- public static void writeObj() throws IOException
- {
- ObjectOutputStream oos =
- new ObjectOutputStream(new FileOutputStream("obj.txt"));
- oos.writeObject(new Person("lisi",20,"kr"));
- oos.close();
- }
- }
复制代码
- import java.io.*;
- class Person implements Serializable
- {
- public static final long serialVersionUID=42L;
- private String name;
- transient int age;
- static String country="cn";
- Person(String name,int age,String country)
- {
- this.name=name;
- this.age=age;
- this.country=country;
- }
- public String toString()
- {
- return name+":"+age+":"+country;
- }
- }
复制代码
|