本帖最后由 赵家阳 于 2013-4-15 17:43 编辑
- <div class="blockcode"><blockquote>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 ObjectStreamDemo {
- public static void main(String[] args) throws Exception{
- writeObj();
- readObj();
- }
- public static void writeObj() throws IOException {
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj1.txt"));
- oos.writeObject(new Person("Jy",12,"china"));
- oos.close();
- }
- public static void readObj() throws IOException, ClassNotFoundException{
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj1.txt"));
- Person p = (Person)ois.readObject();
- System.out.println(p);
- ois.close();
- }
- }
- class Person implements Serializable{
- static final long serialVersionUID = 889;
- String name;
- static int age = 23;
- 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;
- }
- }
复制代码 问题:在对象序列化时,有这么一句话“声明为static和transient类型的成员数据不能被串行化。因为static代表类的状态, transient代表对象的临时数据;”,也就是说,被static修饰的成员,不能被序列化,可是这段代码里为什么country不行?
求指教。。。求序列化知识详情。。。 |