【发布时间】: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 使用反射完成的。然后我查看了Parcel 和Parcelable 源代码,似乎在任何地方都没有明确的句柄。
我必须,还是不必?我真的错过了什么?
【问题讨论】:
标签: java android serialization final parcelable