gpt4 book ai didi

java - 使用 ObjectInputStream 时,在对象反序列化期间是否会调用默认构造函数?

转载 作者:行者123 更新时间:2023-12-02 04:59:53 24 4
gpt4 key购买 nike

刚接触 Java,我正在使用 browxy尝试回答这个问题,但它不允许创建文件。

所以我想知道在下面的代码中,当调用 readObject 时,它是否调用默认构造函数(如果存在)? (另外,Java 中是否有 MemoryStream (C#) 等效项?)

import java.util.*;
import java.io.*;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");

try
{
TestClass tc = new TestClass();

FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tc);
oos.close();

FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);

TestClass tcClone = (TestClass)ois.readObject();

ois.close();
}
catch(Exception e)
{
System.out.println(e);
}

}
}

class TestClass implements Serializable
{
public TestClass()
{
System.out.println("TestClass CTOR");
}
}

最佳答案

I was wondering if in the following code, when readObject gets called, does it call the default constructor (if one exists) or not?

不,它没有..

is there a MemoryStream (C#) equivalent in Java?

ByteArrayInputStreamByteArrayOutputStream

关于java - 使用 ObjectInputStream 时,在对象反序列化期间是否会调用默认构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17137416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com