Serializable Java serialization

xiaoxiao2021-03-06  98

1. Implementing the API released by Serializable returns to change, and makes package-private and private

These two preferred balances cannot be guaranteed.

2, serializable will generate a serial number for each class, the generation basis is the interface name of the class, the class implementation,

Public and protected methods, so as long as you accidentally change a Publish API,

Define a long-type Field called SerialVersionuid, even just adding a getXX,

Let you read the original serialization into something in the file, you can't read it (don't know why you want to take the way?)

3, do not have the constructor to construct the object with serializable, it looks uncomfortable, this is called

Extralinguistic Mechanism, so you should pay attention to maintaining the maintenance function in the constructor when implementing serializable.

Those constant states

4. Added the test burden of posting a new version of the class

5, 1.4 After the version, JavaBeans's persistence uses XML-based mechanism, no longer need serializable

6. When designing the class used to be inherited, try not to achieve serializable, it is used to inherit the interface.

Inherit the serializable. But if the parent class does not implement the serializable interface, it is difficult for the subclass to implement it, especially

When the parent class does not have a parameter-free constructor that does not have a parameter. So, once you decide not to achieve

When the serializable interface is used, it is used to inherit, remember to provide a parametric constructor

7. Is the internal class still do not implement serializable well, unless it is static, (even if the internal class is not suitable

Used to do this type of live)

8. Use a custom serialization method to see this example of this Save a two-way linked list: public class stringlist imports serializable {? Private int size = 0; pipete entry head = NULL; ?? private static class entry imports serializable? {? String data; entry next ;? entry previous;?}? ... // remainder ommitted}

This will cause each element of the linked list and the relationship between elements (the connection between the two-way linked list) is saved, and the better way is to provide a custom serialization as follows:

// String List with a resonable custom serialized formclass StringList implements Serializable {private transient int size = 0;? ?????? // transient private transient Entry head = null ;? // transient // no!?!?? LONGER Serializable !? Private static class entry; ??? entry next; ??? entry previous ;?}? // appends the specified string to the list? public void add (string s) { / *...*/} ;?? / ** ?? * Serialize this stringlist instance ?? * @serial yuchifang ?? * @serialData the size of the list (INTTED) IS Emitted (int ), in the properties void writeObject (ObjectOutputStream S) THROWS IOEXCEPTION? {??? s.defaultWriteObject (); ??? s.writeint (size); ??? @ Write out All Elements In The Proper Order ??? for (entry E = head; e! = null; e = E.NEXT) ????? s.writeObject (e.data) ;?}?? Private Void ReadObject (ObjectInputStream S) THROWS IOEXCEPTION, CLASSNOTFOUNDEEXCE? {??? Int numelements = s.readint (); ??? ??? // read In all elements andd insert the in list ??? for (int i = 0; i

Private static final long serialversion = randomlongvalue;

10, do not need sequential things using transient to get rid of it, don't stay

11, WriteObject / ReadObject overload to complete better serialization

Readresolve with WriteReplace overloaded to complete better maintenance Invariant Controllers

转载请注明原文地址:https://www.9cbs.com/read-124543.html

New Post(0)