zoukankan      html  css  js  c++  java
  • Serializable 和 Parcelable 的区别?

    1.在使用内存的时候,Parcelable 类比 Serializable 性能高,所以推荐使用 Parcelable 类。
    2.Serializable 在序列化的时候会产生大量的临时变量,从而引起频繁的 GC。
    3.Parcelable 不能使用在要将数据存储在磁盘上的情况。尽管 Serializable 效率低点,但在这种情况下,还是建
    议你用 Serializable 。
    实现:
    1 Serializable 的实现,只需要继承 Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
    2 Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接
    口。

    public class MyParcelable 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<MyParcelable> CREATOR
        = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
          return new MyParcelable(in);
        }
        public MyParcelable[] newArray(int size) {
          return new MyParcelable[size];
        }
      };
      private MyParcelable(Parcel in) {
        mData = in.readInt();
      }
    }
    
  • 相关阅读:
    Maven下载Jar包(bat脚本)
    在CentOS7环境下安装Mysql
    在CentOS7下安装JDK1.8
    教你如何进行暗网之旅
    在CentOS7下搭建Hadoop2.9.0集群
    查询IP地址的免费API
    HTTP请求代理类(GET 、 POST 、PUT 、DELETE)
    JAVA 实现 GET、POST、PUT、DELETE HTTP请求
    002---rest_framework认证组件
    001---CBV和restful规范
  • 原文地址:https://www.cnblogs.com/loaderman/p/6525471.html
Copyright © 2011-2022 走看看