java父类没有实现序列化接口,子类实现序列化接口的特殊情况

一.父类实现序列化接口,子类自动继承序列化接口,无需显示申明,即子类序列化时会自动序列化父类对象,反序列化没有问题

二.父类没有实现序列化接口,子类实现序列化接口

1.子类可以被序列化,但是父类无法被序列化.无论如何,子类序列化的过程都可以执行,反序列能够成功则要看父类是否有无参构造器.

JVM会在反序列化子类时自动调用父类的无参构造器创建父类对象,当父类没有无参构造器时,反序列会失败.

所以,子类实现序列化接口,父类没有实现序列化接口时,必须保证父类有无参构造器,才能让子类正常的序列化反序列化

附上个人测试用代码,父类的无参构造器决定了子类能够反序列化成败

import java.io.*;

/**
 * @author Drug
 * @create 2020-05-11 18:22
 */
public class Test {
    public static void main(String[] args) {
        TestBean testBean = new TestBean("1");
        testBean.property1 = "11111";
        testBean.property2 = "22222";
        System.out.println(testBean);
        serialization(testBean, "D:\\testBean.ser");
        System.out.println("序列化成功");
        TestBean deserialization = deserialization("D:\\testBean.ser");
         System.out.println("反序列化成功");
        System.out.println(deserialization);
    }

    /**
     * 序列化
     * @param src 目标对象
     * @param target 文件路径
     */
    public static void serialization(TestBean src, String target) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(target);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(src);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 反序列化
     * @param target 文件路径
     * @return
     */
    public static TestBean deserialization (String target){
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(target);
            ois = new ObjectInputStream(fis);
            Object o = ois.readObject();
            return (TestBean) o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class BaseBean {
    public String property1;
    public String property2;

    //带参构造
    public BaseBean(String id) {
    }
    //无参构造,如没有无参构造,子类反序列化会失败
    public BaseBean(){
    }
}

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 + '\'' +
                '}';
    }

}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,序列后可以使用类进行反序列接收。当实现了 Serializable 接口并进行了序列类也自动继承了 Serializable 接口。因此,类对象可以用来接收类对象的反序列结果。 以下是一个简单的示例代码: ```java import java.io.*; class Parent implements Serializable { private static final long serialVersionUID = 1L; String parentName; public Parent(String parentName) { this.parentName = parentName; } } class Child extends Parent { private static final long serialVersionUID = 1L; String childName; public Child(String parentName, String childName) { super(parentName); this.childName = childName; } } public class SerializationExample { public static void main(String[] args) { // 序列 try { Child child = new Child("Parent", "Child"); FileOutputStream fileOut = new FileOutputStream("serialization.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(child); out.close(); fileOut.close(); System.out.println("对象已被序列并保存到 serialization.ser 文件中"); } catch (IOException e) { e.printStackTrace(); } // 反序列 try { FileInputStream fileIn = new FileInputStream("serialization.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); Child child = (Child) in.readObject(); in.close(); fileIn.close(); System.out.println("对象已从 serialization.ser 文件中反序列"); System.out.println("类名称: " + child.parentName); System.out.println("类名称: " + child.childName); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ``` 在上述代码中,类 Parent 实现了 Serializable 接口类 Child 继承了 Parent。首先,创建一个 Child 对象并将其序列保存到文件 serialization.ser 中。然后,从该文件中反序列 Child 对象,并打印出类和类的名称。 运行代码,输出结果为: ``` 对象已被序列并保存到 serialization.ser 文件中 对象已从 serialization.ser 文件中反序列 类名称: Parent 类名称: Child ``` 可以看到,通过类 Child 对象成功接收了类 Parent 的反序列结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值