1.对象实现Serializable接口

2.将对象通过对象输出流输出,在反序列

    public Object getClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bio = new ByteArrayOutputStream();//字节流的形式输出
        ObjectOutputStream oos = new ObjectOutputStream(bio);//对象流接受字节流
        oos.writeObject(this);//将当前对象序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bio.toByteArray());//接收一个字节数组
        ObjectInputStream ois = new ObjectInputStream(bis);//将字节流读入对象流
        DeepSheep o = (DeepSheep) ois.readObject();
        return o;
    }

 

posted on 2022-05-10 22:06  upupup-999  阅读(34)  评论(0)    收藏  举报