java 如何反序列化对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6379912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:40:13  来源:igfitidea点击:

how to deserialize object?

javaserialization

提问by dave

I have a class called Flight The Flight class when instantiated, instantiates another class called SeatingChart, and SeatingChart also instantiates another class and so on and so forth.

我有一个名为 Flight 的类,实例化时 Flight 类实例化另一个名为 SeatingChart 的类,SeatingChart 也实例化另一个类,依此类推。

public class Flight implements Serializable
{

    SeatingChart sc = new SeatingChart(); seating 
    //WaitingList wl = new WaitingList();
}

public class SeatingChart extends ListAndChart implements PassengerList, Serializable
{
    public static final int NOT_FOUND = 42;

    Passenger [] pass = new Passenger[40];
}

public class Passenger implements Serializable
{

    private String firstName, lastName, fullName;

    public String getName()
    {   
        fullName = firstName + " " + lastName;
        return fullName;
    }
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
}

I have another method in another class that deserializes the object saved in the disk

我在另一个类中有另一种方法可以反序列化保存在磁盘中的对象

public void actionPerformed(ActionEvent evt)
            {  
                Serialization.deserialize(sw101); <--- sw101 is a Flight object
                .
                .
                .
            }

//code for deserialization
public static void deserialize(Flight sw101)
    {
        String filename = "serialized.ser";

        sw101 = null;

        FileInputStream fis = null;
        ObjectInputStream in = null;

        try
        {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            sw101 = (Flight)in.readObject();
            System.out.println("sw101" + sw101.toString());
            in.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }   
    }

my question is when I assign sw101 the serialized object, everything sw101 instantiated in the beginning like the SeatingChart sc object also get whatever is saved in the file without me doing anything as long as those objects all implement the Serializable interface? If so why is my code not working? what am I doing wrong?

我的问题是,当我为 sw101 分配序列化对象时,所有 sw101 像 SeatingChart sc 对象一样在开始时实例化的所有内容也会得到文件中保存的任何内容,只要这些对象都实现了 Serializable 接口,我无需做任何事情?如果是这样,为什么我的代码不起作用?我究竟做错了什么?

采纳答案by trutheality

It looks like you're trying to return through a reference parameter (C/C++ background?)

看起来您正在尝试通过引用参数返回(C/C++ 背景?)

That never works in Java. All parameters (including references) are passed by value. Once you do

这在 Java 中永远行不通。所有参数(包括引用)都是按值传递的。一旦你这样做

sw101=null;

you lose the reference to the object that was passed in.

你失去了对传入对象的引用。

Your deserializefunction should returnthe flight object instead.

您的deserialize函数应该返回飞行对象。

(Technically there is a way to simulate returning through the arguments in Java by using an array, but it leads to unnecessarily complicated code)

(从技术上讲,有一种方法可以通过使用数组来模拟 Java 中的参数返回,但这会导致不必要的复杂代码)

回答by Felipe Cruz

In java, all parameters are passed as values.. so the answer to your last question is no. sw101is a reference to a copy.

在java中,所有参数都作为值传递..所以你最后一个问题的答案是否定的。sw101是对副本的引用。

As said, you have to return your deserialized object to make it work.

如前所述,您必须返回反序列化的对象才能使其工作。

check this article: parameter passing in java

检查这篇文章:java中的参数传递