黑马程序员技术交流社区
标题:
毕老师课程中ObjectStream的transient关键字的疑问
[打印本页]
作者:
touch_world
时间:
2014-11-12 12:51
标题:
毕老师课程中ObjectStream的transient关键字的疑问
import java.io.*;
public class ObjectStreamDemo {
public static void main(String[] args)throws Exception
{
writeObject();
readObject();
}
public static void readObject() throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:/Person.obj"));
Person p1=(Person)ois.readObject();
Person p2=(Person)ois.readObject();
s.p(p1);
s.p(p2);
ois.close();
}
public static void writeObject() throws Exception
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/Person.obj"));
oos.writeObject(new Person("lisi",20,"cn"));
oos.writeObject(new Person("wangwu",30,"kr"));
oos.close();
}
}
class Person implements Serializable//记得要实现一下接口
{
static final long serialVersionUID = 43L;
private String name;
private transient int age;//非常像是static final的感觉 无法修改,无法序列
private static String country = "usa";//没有被序列化
@SuppressWarnings("static-access")
Person(String name,int age,String country)
{
this.name=name;
this.age=age;
this.country=country;
}
public String toString()
{
return name+"::"+age+country;
}
}
复制代码
transient 这个关键字修饰后的变量,不可以被序列化 很像static
使用的过程中发现无法被修改,又非常像final修饰
大神能不能告诉我下 transient == static fianl 变成全局常量
底层是不是这样呀
疑问是transient 有没有修改的方法
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2