日期:2014-05-20 浏览次数:20799 次
/** * 该类对象可以被序列化 * @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; } ...... }