【问题标题】:Android Parcelable finalsAndroid Parcelable 决赛
【发布时间】:2014-08-14 17:10:01
【问题描述】:

所以,我有以下类,我想使用 Android Parcelable 对其进行序列化

public class Parent{
   public static final int x; //primtive type
   public static final Animal y; //another percelable
   public static final String z; //object type
   public class Inner{
       public static final int a; //primtive type
       public static final Animal b; //another percelable
       public static final String c; //object type
   }
   private int classIntState; //primtive type
   private Animal classAnimalState; //another percelable
   private String classObjectState; //object type
}

我知道类变量的练习。但是在内部类的决赛和决赛上遇到麻烦。

做了一些额外的研究,我研究了BlutoothDevice 类,它恰好也是 Parcelable 并且有一堆常量。

而且,我在源代码中找到了这个,

    public static final Parcelable.Creator<BluetoothDevice> CREATOR =
        new Parcelable.Creator<BluetoothDevice>() {
    public BluetoothDevice createFromParcel(Parcel in) {
        return new BluetoothDevice(in.readString());
    }
    public BluetoothDevice[] newArray(int size) {
        return new BluetoothDevice[size];
    }
   };

   public void writeToParcel(Parcel out, int flags) {
       out.writeString(mAddress);
   }

   BluetoothDevice(String address) {
        getService();  // ensures sService is initialized [nothing to do with parcelable]
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
        }

        mAddress = address;
    }

似乎这些人完全忽略了 Parcelable 实现中的所有这些决赛/常量。

这让我有点困惑,所以研究了 Java Serialize finals 并在堆栈溢出时发现了这个 discussion。我从他们那里了解到的是,它是由 JVM 使用反射完成的。然后我查看了ParcelParcelable 源代码,似乎在任何地方都没有明确的句柄。

我必须,还是不必?我真的错过了什么?

【问题讨论】:

    标签: java android serialization final parcelable


    【解决方案1】:

    似乎这些人在他们的 Parcelable 实现中完全忽略了所有这些决赛/常量。

    好吧,如果您查看BluetoothDevicesource,您会注意到所有final 变量都是在构造函数之外定义的。所以当return new BluetoothDevice(in.readString())被调用时,构造函数定义的所有final变量都被初始化为各自的值,然后构造函数BluetoothDevice(String address)被调用。

    我想说的是,这些值不是从Parcel 写入或读取的。它们只是在构造函数被ParentCREATOR 调用之前由类初始化(尽管您尚未在问题的Parent 源代码中定义一个)。

    现在,假设您根据参数化构造函数中的参数初始化final 变量的值。例如,

    public Parent(int x, Animal y, String z) {
    
      this.x = x;
      this.y = y;
      this.z = z;
    
    }
    

    在这种情况下,您需要将xyz 写入writeToParcel() 中的Parcel,就像您写入任何non-final 值一样。

    然后在你的Parent类'CREATOR

    public static final Parcelable.Creator<Parent> CREATOR =
        new Parcelable.Creator<Parent>() {
        public Parent createFromParcel(Parcel in) {
    
            //read values from Parcel
            int intParam = in.readInt();
            Animal animalParam = in.readParcelable(Animal.class.getClassLoader());
            String stringParam = in.readString();
    
            //create parent
            Parent parent = new Parent(intParam, animalParam, stringParam);
    
            return parent;
        }
        public Parent[] newArray(int size) {
            return new Parent[size];
        }
     };
    

    也就是说,你读取变量的值,然后使用构造函数对其进行赋值。同样,这与变量不是final 几乎相同。我不认为它们有什么区别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      相关资源
      最近更新 更多