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();
      }
    }
    
  • 相关阅读:
    unittest中常用的几个断言
    unittest中忽略某些测试用例的执行
    unittest测试套件
    unittest中的Empty suite错误
    找水王
    SCRUM冲刺day04
    SCRUM冲刺day03
    SCRUM冲刺day02
    SCRUM冲刺day01
    学习进度条week13
  • 原文地址:https://www.cnblogs.com/loaderman/p/6525471.html
Copyright © 2011-2022 走看看