Java序列化与Serializable(三)父类实现如果未实现序列化,子类表现

博客探讨了Java中父类构造函数对序列化的影响。指出子类承担责任需父类有无参构造方法,通过两种情况测试:父类无无参构造函数时,序列化成功但反序列化报错,父类属性未序列化;父类有无参构造时,虽可序列化反序列化,但父类属性未参与该过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


父类未实现序列化

To allow subtypes of non-serializable classes to be serialized, the
* subtype may assume responsibility for saving and restoring the
* state of the supertype's public, protected, and (if accessible)
* package fields.  The subtype may assume this responsibility only if
* the class it extends has an accessible no-arg constructor to
* initialize the class's state.  It is an error to declare a class
* Serializable if this is not the case.  The error will be detected at
* runtime. <p>

The subtype may assume this responsibility only if * the class it extends has an accessible no-arg constructor to * initialize the class's state.

也就是说需要父类要有无参的构造方法。

我们试一下如果是没有父类无参的构造函数会怎么样

因为父类如果没有写构造函数的话,默认会有一个无参的构造函数,所以我们这样写

第一种情况  父类没有无参的构造函数

父类:

public class BaseBean   {
   public String property1;
   public String property2;

   public BaseBean(String id){

   }
}

子类

public class TestBean extends BaseBean  implements Serializable{
        public String desc;
    public static final int serialVersionUID = 1;
    public TestBean(String id) {
        super(id);
    }

    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                '}';
    }

}

这个时候序列化的过程没有出什么问题,但是反序列的时候开始报错了

//序列化之后通过给父类再加上默认构造方法,这个时候反序列化可以看到其实即便父类没有默认构造参数,序列化的过程也是

成功了的。但是1,不能反序列化,2,父类的属性没有序列化到,只序列化了子类的属性

 java.io.InvalidClassException: calc.superdy.ttest.test.TestBean; no valid constructor

 

第二种情况 父类有无参构造

通过上面的例子,可以知道,如果父类没有无参构造函数,则序列化过程不能成功

下面加下无参构造,看下情况

public class BaseBean   {
   public String property1;
   public String property2;

   public BaseBean(String id){

   }

   public BaseBean(){}

}
public class TestBean extends BaseBean  implements Serializable{
        public String desc;
    public static final int serialVersionUID = 1;
    public TestBean(String id) {
        super(id);
    }

    @Override
    public String toString() {
        return "TestBean{" +
                "desc='" + desc + '\'' +
                ", property1='" + property1 + '\'' +
                ", property2='" + property2 + '\'' +
                '}';
    }

}
   public void serialization(View view) {
        try {

            TestBean testBean = new TestBean("");
            testBean.property1 = "属性88999988";
            testBean.property2 = "属性88999988";
            testBean.desc = "我是testbean88999988";

            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();
        }
    }

这个时候打印结果为:

 deserializationTestBean{desc='我是testbean88999988', property1='null', property2='null'}

也就是说,虽然可以序列化反序列化成功,但是父类的属性其实并没有参与到序列化反序列化的过程中来


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值