Serializable、Externalizable、Parcelable实现

1、Java的Serializable与Externalizable:
区别:
Serializable接口
优点:内建支持、易于实现
缺点:占用空间过大、额外的开销速度慢


Externalizable接口
优点:用户选择性序列化,开销较少
缺点:需用户选择控制序列化。


Serializable系统自动序列化对象状态(成员变量),transient,static变量不序列化;
Externalizable用户全权控制序列化过程(成员变量选择性序列化,可对变量先压缩加密再序列化),transient,static变量也可序列化


class Demo implements Serializable
{
    int              a;
    transient int    b;
    static int       c;
    String           d;
    transient String e;
    
    /**
     * @author xiazehong
     * @date 2014年12月4日 上午8:41:40
     */
    public Demo(int a, int b, int c, String d, String e)
    {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
    }
}


class Demo implements Externalizable
{
    int              a;
    transient int    b;
    static int       c;
    String           d;
    transient String e;
    
    public Demo()
    {
        
    }
    
    /**
     * @author xiazehong
     * @date 2014年12月4日 上午8:41:40
     */
    public Demo(int a, int b, int c, String d, String e)
    {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
    }
    
    @Override
    public void writeExternal(ObjectOutput oo)
        throws IOException
    {
        oo.writeInt(a);
        oo.writeInt(b);
        oo.writeInt(c);
        oo.writeObject(d);
        oo.writeObject(e);
    }
    
    @Override
    public void readExternal(ObjectInput oi)
        throws IOException, ClassNotFoundException
    {
        a = oi.readInt();
        b = oi.readInt();
        c = oi.readInt();
        d = (String)oi.readObject();
        e = (String)oi.readObject();
    }
}


public class Main
{
    public static void main(String[] args)
    {
//        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/serial.txt")))
//        {
//            Demo demo = new Demo(1, 2, 3, "xzh", "cmm");
//            oos.writeObject(demo);
//        }
//        catch (IOException e)
//        {
//            e.printStackTrace();
//        }
        
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/serial.txt")))
        {
            Demo demo = (Demo)ois.readObject();
            System.out.println("a=" + demo.a);
            System.out.println("b=" + demo.b);
            System.out.println("c=" + demo.c);
            System.out.println("d=" + demo.d);
            System.out.println("e=" + demo.e);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }



2、Andriod的Parcelable
class Demo implements Parcelable 
{
     private int mData;


     public int describeContents() 
     {
         return 0;
     }


     public void writeToParcel(Parcel out, int flags) 
     {
         out.writeInt(mData);
     }


     public static final Parcelable.Creator<Demo> CREATOR = new Parcelable.Creator<Demo>() 
     {
         public Demo createFromParcel(Parcel in) 
         {
             return new Demo(in);
         }


         public Demo[] newArray(int size) 
         {
             return new Demo[size];
         }
     };
     
     private Demo(Parcel in) 
     {
         mData = in.readInt();
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值