看下android源码中的注释:
父类实现序列化,子类则也可以序列化
Android-added: Notes about serialVersionUID, using serialization judiciously, JSON.
Serializability of a class is enabled by the class implementing the * java.io.Serializable interface. Classes that do not implement this * interface will not have any of their state serialized or * deserialized. All subtypes of a serializable class are themselves * serializable. The serialization interface has no methods or fields * and serves only to identify the semantics of being serializable. <p>
翻译:实现Serializable 接口就能序列化。实现serializable的类的子类也是serializable的。
这个序列化接口没有方法或者fields,只是语法标记为可以序列化
上面说如果父类是序列化的,则子类也是序列化的,我们来做一个测试
父类
public class BaseBean implements Serializable {
public static final int serialVersionUID = 1;
public String property1;
public String property2;
}
子类
public class TestBean extends BaseBean {
public String desc;
@Override
public String toString() {
return "TestBean{" +
"desc='" + desc + '\'' +
", property1='" + property1 + '\'' +
", property2='" + property2 + '\'' +
'}';
}
}
测试代码
public void serialization(View view) {
try {
TestBean testBean = new TestBean();
testBean.property1 = "属性22882288";
testBean.property2 = "属性6666";
testBean.desc = "我是testbean";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("/sdcard/aaatest.txt"));
objectOutputStream.writeObject(testBean);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deserialization(View view) {
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("/sdcard/aaatest.txt"));
TestBean testBean = (TestBean) objectInputStream.readObject();
Log.i(TAG,"deserialization"+testBean.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
实测下来,可以正常序列化和反序列化,没有问题