Android面试简集(序列化)

先推荐一个写的很详细的文章(自问做不到这么详细)
https://www.jianshu.com/p/208ac4a71c6f
序列化:内存中对象存储到磁盘中
反序列化:磁盘中对象读取到内存中
Serializable:是Java提供的一个序列化的空接口,专门为对象提供标准序列化和反序列化操作的,使用比较简单,只需要在类中实现Serializable接口即可
缺点:比较耗内存

image

Serializable

Parcelable
是android中提供的一个序列化接口,性能比Serializable好,在内存开销较小,所以在内存间数据传输时推荐使用Parcelable
缺点:操作使用比较麻烦(附接口定义代码)

/**
 * Flag for use with {@link #writeToParcel}: the object being written
 * is a return value, that is the result of a function such as
 * "<code>Parcelable someFunction()</code>",
 * "<code>void someFunction(out Parcelable)</code>", or
 * "<code>void someFunction(inout Parcelable)</code>".  Some implementations
 * may want to release resources at this point.
 */
public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;

/**
 * Flag for use with {@link #writeToParcel}: a parent object will take
 * care of managing duplicate state/data that is nominally replicated
 * across its inner data members.  This flag instructs the inner data
 * types to omit that data during marshaling.  Exact behavior may vary
 * on a case by case basis.
 * @hide
 */
public static final int PARCELABLE_ELIDE_DUPLICATES = 0x0002;

/*
 * Bit masks for use with {@link #describeContents}: each bit represents a
 * kind of object considered to have potential special significance when
 * marshalled.
 */

/**
 * Descriptor bit used with {@link #describeContents()}: indicates that
 * the Parcelable object's flattened representation includes a file descriptor.
 *
 * @see #describeContents()
 */
public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;

/**
 * Describe the kinds of special objects contained in this Parcelable
 * instance's marshaled representation. For example, if the object will
 * include a file descriptor in the output of {@link #writeToParcel(Parcel, int)},
 * the return value of this method must include the
 * {@link #CONTENTS_FILE_DESCRIPTOR} bit.
 *  
 * @return a bitmask indicating the set of special object types marshaled
 * by this Parcelable object instance.
 *
 * @see #CONTENTS_FILE_DESCRIPTOR
 */
public int describeContents();

/**
 * Flatten this object in to a Parcel.
 * 
 * @param dest The Parcel in which the object should be written.
 * @param flags Additional flags about how the object should be written.
 * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
 */
public void writeToParcel(Parcel dest, int flags);

/**
 * Interface that must be implemented and provided as a public CREATOR
 * field that generates instances of your Parcelable class from a Parcel.
 */
public interface Creator<T> {
    /**
     * Create a new instance of the Parcelable class, instantiating it
     * from the given Parcel whose data had previously been written by
     * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
     * 
     * @param source The Parcel to read the object's data from.
     * @return Returns a new instance of the Parcelable class.
     */
    public T createFromParcel(Parcel source);
    
    /**
     * Create a new array of the Parcelable class.
     * 
     * @param size Size of the array.
     * @return Returns an array of the Parcelable class, with every entry
     * initialized to null.
     */
    public T[] newArray(int size);
}

/**
 * Specialization of {@link Creator} that allows you to receive the
 * ClassLoader the object is being created in.
 */
public interface ClassLoaderCreator<T> extends Creator<T> {
    /**
     * Create a new instance of the Parcelable class, instantiating it
     * from the given Parcel whose data had previously been written by
     * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and
     * using the given ClassLoader.
     *
     * @param source The Parcel to read the object's data from.
     * @param loader The ClassLoader that this object is being created in.
     * @return Returns a new instance of the Parcelable class.
     */
    public T createFromParcel(Parcel source, ClassLoader loader);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容