关于transient的困惑
前段时间复习了一下对象的序列化技术,其中涉及到了java语言规范中的transient关键字的用法,根据官方的解释,如果一个对象的属性用该关键字修饰,那么在序列化对象的时候,将忽略该属性(即不会进行存储,反序列化时该属性值将丢失)。
1.新建一个实现Serializable接口的类
/** * 该类对象可以被序列化 * @author yuanli * */public class Book implements Serializable { /** * */ private static final long serialVersionUID = 3173949523199923358L; private int id; private String name; private transient float price; public Book() { super(); } public Book(int id, String name, float price) { super(); this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "id=" + this.id + ",name=" + this.name + ",price=" + this.price; }}public class Test {public static void main(String[] args) { Test t = new Test(); t.testTransient();}public void testTransient() { try { Book book = new Book(1,"aa",12.5f); System.out.println("book序列化之前: " + book.toString()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); //序列化book对象 oos.writeObject(book); //反序列化book对象 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); Book book_serializable = (Book)ois.readObject(); System.out.println("book反序列化之后:" + book_serializable.toString()); } catch (Exception e) { e.printStackTrace(); } }}public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable{ private transient Entry<E> header = new Entry<E>(null, null, null); private transient int size = 0; /** * Constructs an empty list. */ public LinkedList() { header.next = header.previous = header; } /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); } ...... /** * Returns the number of elements in this list. * * @return the number of elements in this list */ public int size() { return size; } ......}
/** * Save the state of this <tt>LinkedList</tt> instance to a stream (that * is, serialize it). * * @serialData The size of the list (the number of elements it * contains) is emitted (int), followed by all of its * elements (each an Object) in the proper order. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden serialization magic s.defaultWriteObject(); // Write out size s.writeInt(size); // Write out all elements in the proper order. for (Entry e = header.next; e != header; e = e.next) s.writeObject(e.element); } /** * Reconstitute this <tt>LinkedList</tt> instance from a stream (that is * deserialize it). */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject(); // Read in size int size = s.readInt(); // Initialize header header = new Entry<E>(null, null, null); header.next = header.previous = header; // Read in all elements in the proper order. for (int i=0; i<size; i++) addBefore((E)s.readObject(), header); }
[解决办法]