gpt4 book ai didi

java - 为什么在反序列化期间不调用默认构造函数

转载 作者:行者123 更新时间:2023-11-30 01:51:49 29 4
gpt4 key购买 nike

为什么在反序列化机制期间不调用默认构造函数?

关注讨论和评论,但没有找到满意的答案。

我在反序列化过程中理解了

它通过调用其默认构造函数来创建 Object 类的实例,并在创建的实例的帮助下,使用 ReflectionFactory 类的 newConstructorForSerialization() 方法创建 Parent 和 Child 类的实例,该方法在内部创建实例而不调用类的构造函数。

这样实现的原因是什么?

 public class EmployeeAddress/* implements Serializable */{

/**
*
*/
/*private static final long serialVersionUID = 4455988544392627515L;*/
protected String address;

public EmployeeAddress() {
System.out.println(this.address);
System.out.println("EmployeeAddress constructor has invoked with no-args");
}

public EmployeeAddress(String address) {
this.address = address;
System.out.println(this.address);
System.out.println("EmployeeAddress constructor has invoked with args");
}

// setter / getter
}

public class Employee extends EmployeeAddress implements Serializable{
/**
*
*/
/*public static final long serialVersionUID = 151736201136818635L;*/
private static final long serialVersionUID = 2L;

private String name;
private int age;
private String designation;
private double salary;
private PermanentEmployee pEmpoyee;

private transient long empId;

/*public Employee(String address) {super(address);}*/

public Employee(String name, int age, PermanentEmployee pEmpoyee, String designation, String address, long empId) {
super(address);
this.name = name;
this.age = age;
this.pEmpoyee = pEmpoyee;
this.designation = designation;
this.salary = 2000.00 + pEmpoyee.getBonus();
this.empId = empId;
}

// setter / getter
}


public class PermanentEmployee implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6153718980149785271L;

private double bonus;

public PermanentEmployee() {}

public PermanentEmployee(double bonus) {
this.bonus = bonus;
}

// setter / getter
}


public class SerializationDemo {
public static void main(String[] args) {

PermanentEmployee pEmpoyee = new PermanentEmployee(1000.00);

com.serialization.first.Employee emp = new com.serialization.first.Employee("XYY", 30, pEmpoyee, "Leader", "Nagpur", 2001);
WriterAndReader.write(emp, "emp.ser");

System.out.println("Serailization processed..for com.serialization.first.Employee ");


com.serialization.first.Employee emp1 = (com.serialization.first.Employee) WriterAndReader.read("emp.ser");

System.out.println("Name: "+ emp1.getName()
+ ", Age: "+ emp1.getAge()
+ ", Designation: "+ emp1.getDesignation()
+ ", Salary: " + emp1.getSalary()
+ ", Address: " + emp1.getAddress()
+ ", EmpId: " + emp1.getEmpId());

System.out.println("Deserailization processed.. for com.serialization.first.Employee ");
}
}

Output:

null
EmployeeAddress constructor has invoked with no-args
Name: XYY, Age: 30, Designation: Leader, Salary: 3000.0, Address: null, EmpId: 0
Deserailization processed.. for com.serialization.first.Employee

最佳答案

因为这是序列化的工作方式。它充当隐藏的构造函数,正如 Joshua Bloch 在“Effective Java”中所解释的:https://qtips.github.io/2017/08/effective-java-chapter-11

有一种机制可以用 readObjectreadResolve 方法来“替代”构造函数调用。在那里,您可以处理流损坏/操作,保护类不变量并恢复不可序列化字段的状态:Java serialization: readObject() vs. readResolve()

对于背后的原因,我很快就能想到的是,并非所有类都声明无参数构造函数。因此您不能依赖它来创建任何类型的对象。

关于java - 为什么在反序列化期间不调用默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55741134/

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