我正在使用 Google's LatLng class来自 v2 Google Play 服务。该特定类是最终类,未实现 java.io.Serializable
。有什么方法可以使 LatLng
类实现 Serializable
?
public class MyDummyClass implements java.io.Serializable {
private com.google.android.gms.maps.model.LatLng mLocation;
// ...
}
我不想声明 mLocation
transient。
最佳答案
它不是 Serializable
但它是 Parcelable
,如果这是一个选项的话。如果没有,您可以自己处理序列化:
public class MyDummyClass implements java.io.Serialiazable {
// mark it transient so defaultReadObject()/defaultWriteObject() ignore it
private transient com.google.android.gms.maps.model.LatLng mLocation;
// ...
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeDouble(mLocation.latitude);
out.writeDouble(mLocation.longitude);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
mLocation = new LatLng(in.readDouble(), in.readDouble());
}
}
关于java - 如何序列化第三方不可序列化的最终类(例如谷歌的 LatLng 类)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14220554/