java – 非可序列化父类的Serializable子类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 非可序列化父类的Serializable子类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3589字,纯文字阅读大概需要6分钟。
内容图文
![java – 非可序列化父类的Serializable子类](/upload/InfoBanner/zyjiaocheng/704/aeeb6985a8f34839964505a08c0bfef4.jpg)
我正在使用android / java中的Location子类序列化打砖块
位置不可序列化.
我有一个名为FALocation的第一个子类,它没有任何实例变量.我已声明它可序列化.
然后我有一个名为Waypoint的第二个类看起来像这样:
public class Waypoint extends FALocation implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/* Class variables *******************************************************/
private static int CLASS_VERSION=1; //Used to version parcels
/* Instance variables ****************************************************/
private transient String type=DataHelper.PT_TYPE_US;
private transient String country;
private transient String name=null;
private transient String description=null;
private transient int elevation = 0;
private transient int population = 0; // Afterthought, added to match the DB structure
/* Constructors **********************************************************/
public Waypoint() {
super();
}
public Waypoint(double lat, double lon, String name, String description) {
super(lat, lon);
this.setName(name);
this.setDescription(description);
}
public Waypoint(Location l) {
super(l);
}
public Waypoint(String provider) {
super(provider);
}
/* Implementing serializable */
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
Log.v("DroidFA", "Serialising \"%s\" (v%d).", Waypoint.class.getSimpleName(), CLASS_VERSION);
out.writeInt(CLASS_VERSION);
out.writeObject(type);
out.writeObject(country);
out.writeObject(name);
out.writeObject(description);
out.writeInt(elevation);
out.writeInt(population);
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
int serialClassVersion = in.readInt();
Log.v("DroidFA", "Deserialising \"%s\" (v%d).", Waypoint.class.getSimpleName(),serialClassVersion);
type = (String) in.readObject();
country = (String) in.readObject();
name = (String) in.readObject();
description = (String) in.readObject();
elevation = in.readInt();
population = in.readInt();
}
}
序列化工作正常.
Deseriamization产生跟随异常(leg对象包含一个航点):
10-05 13:50:35.259: WARN/System.err(7867): java.io.InvalidClassException: android.location.Location; IllegalAccessException
10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectInputStream.resolveConstructorClass(ObjectInputStream.java:2010)
10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2095)
10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
10-05 13:50:35.278: WARN/System.err(7867): at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
10-05 13:50:35.278: WARN/System.err(7867): at com.droidfa.navigation.Leg.readObject(Leg.java:262)
.../...
解决方法:
是否绝对有必要序列化位置?也许你可以将它标记为瞬态,并在反序列化对象后动态获取它. (Anyway, from the documentation ):
Q: If class A does not implement Serializable but a subclass B implements Serializable, will the fields of class A be serialized when B is serialized?
A: Only the fields of Serializable objects are written out and restored. The object may be restored only if it has a no-arg constructor that will initialize the fields of non-serializable supertypes. If the subclass has access to the state of the superclass it can implement writeObject and readObject to save and restore that state.
因此,如果子类可以访问其非可序列化超类的字段,则可以使用writeObject和readObject协议来实现序列化.否则,将存在无法序列化的字段.
内容总结
以上是互联网集市为您收集整理的java – 非可序列化父类的Serializable子类全部内容,希望文章能够帮你解决java – 非可序列化父类的Serializable子类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。