Sequence of Java Learning (2)

zhaozj2021-02-16  72

Object continuity

In the sequence of serialization of Java learning (1), you already know some of the meaning of the serialization mechanism and how to achieve serialization. In this article, we will go deep into the internal mechanism to see how it works!

For example, please:

Suppose it is now written two classes, an Employee (employee), a Manager (manager class), then creates their objects, you need to specify a secretary for Manager when you create a Manager object, and the secretary is also an EMPLOYEE, where we are here The secretary of MANager is made with the ready-made Employee object, that is to say that the object reference to an Employee needs to be included in the Manager, as follows:

Employee Harry = New Employee ("Harry Hacke", .......); // is the employee is a secretary

Manager manager1 = new manager ("tony tester", .......); // manager object

Manager1.setsecretary (HARRY); // Setting the secretary for Harry

Now, in memory actually created two objects, an EMPLOYEE, a Manager and Manager contains a reference to the Employee object, which is a reference, when we change the above these write disks, Harry data It was preserved twice, that is, we got a complete copy of the Harry object in Manager. This is of course not what we want to see. For example, we have to give Harry salary, of course, do not want to search for all other copies of the object. In other words, we hope that the object layout on the disk and the object layout in the memory remain complete. This is "Object Sustainability"!

You may think that you can save the memory address of the Secretary object, can't do, because each load object may use and the originally distinct memory address!

However, now, Java solves this problem is to adopt serialization mechanisms, below is serialized specific algorithms:

1. All objects saved to disk have a serial number (1, 2, 3, etc.)

2. When you want to save an object, check if the object has been saved.

3. If you have saved before, you only need to write "the same as the object already saved" "tags; otherwise, the object is saved.

Through the above steps, the problem of "subject sustainability" is solved!

Watch an example! (Under JDK1.4, pass)

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

public class ObjectRefTest {public static void main (String [] args) {Employee harry = new Employee ( "Harry Hacker", 50000); Manager manager1 = new Manager ( "Tony Tester", 80000); manager1.setSecretary (harry); Employee [] staff = new Employee [2]; staff [0] = harry; staff [1] = manager1; try {ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ( "employee.dat")); out.writeObject (staff) Out.close (); ObjectInputStream IN = New ObjectInputStream (New FileInputStream ("Employee.dat"); Employee [] newstaff = (Employee []) in.readObject (); in.close (); / ** * Take the Harry object to salary * will reflect * / news on Secretary * / news (10); for (int i = 0; i

class Employee implements Serializable {public Employee () {} public Employee (String n, double s) {name = n; salary = s;} / ** * a raise * / public void raiseSalary (double byPercent) {double raise = Salary * bypercent / 100; salary = raise;} public string toString () {return getclass (). getname () "[name =" name ", Salary =" Salary "];} private string Name; Private Double Salary;} Class Manager Extends Employee {Public Manager (String N, Double S) {Super (n, s); Secretary = NULL;} / ** * Settings Secretary * / Public Void SetSecretary Employee s) {secretary = s;} public string toString () {return super.tostring () "[secretary =" secretary "]"] "} // secretary represents secretary private Employee secondary;

The above is the default behavior of serialization. In the actual programming, we will also encounter a lot of cases that lead to the default behavior errors, in the next part, we will see how to modify the default serialization! thanks for reading!

转载请注明原文地址:https://www.9cbs.com/read-15003.html

New Post(0)