Learn Java experience (1)

zhaozj2021-02-16  54

1. "I can't believe this is true!"

I am developing a Swing application that obtains data through the RMI server in the background, then uses new data notification the front desk display program, the front desk is reversed can also notify the background to save new data after being modified. I used the Sequence view of the specific process:

Where the C / S architecture passes the serialized string of a Data Model of a visual element. The front desk uses a PANEL class to display all the current visual elements. The PANEL class is as follows:

The problem now is that the system just started, then the display will be data just read from the RMI server, which will be deserialized as VisualObjects so the system will display a single view of the previous closure of the system to the user. However, I found that if this time, the user deletes the VisualObject on the panel, then the process of deleting this delete will be used with the following program, it will fall into a trap:

Public Void RemovevisualObject (VisualObject Vo) {

IF (vo == null) return;

Panel.Remove (VO);

}

When running the program, you will find that there is no corresponding corresponding time, so I don't die, decide to debug, list all the elements above the current Panel, and the VO is compared, see the incoming VO is No problem, the code is as follows:

Public Void RemovevisualObject (VisualObject Vo) {

IF (vo == null) return;

For (int i = 0; i

Tempvo = (vo) Panel.getComponent (i);

IF (Vo.Equals (Tempvo)) {

System.out.println ("Find Equals");

}

}

System.out.println ("No Equals");

}

I think, if you find no equals, it is the problem that I passed into the parameter. The result is that Find Equals, why? Since my incoming object exists on the panel, why is my delete operation unsuccessful? On the occasion of scratching, I decided to drilled into the end, so I took a step forward:

Public Void RemovevisualObject (VisualObject Vo) {

IF (vo == null) return;

For (int i = 0; i

Tempvo = (vo) Panel.getComponent (i);

IF (Vo.Equals (Tempvo)) {

System.out.println ("Find Equals");

Panel.Remove (Tempvo);

}

}

System.out.println ("No Equals");

}

Another one this shocked running program, click delete, and the result. . . . I can't believe it, my heart is a mad, I finally got it. If I don't want to shout in the office, "I ...", forget, don't be busy to celebrate, start to analyze the reason. After thinking about it, I finally understood some key questions: 1, that is, my object is saved by serialization.

2. When my object is restored, it replies through the reverse sequence of sequence, so that the content in the object will definitely change. But don't forget, the address of the object is definitely different. This is the key point.

3. When Panel deletes an object, it is turned out through the address. Although I overloaded the equals method of the object, it is absolutely equal to it. But I don't have a way to suffer from Java API.

4. You can only call the equals method to compare the equals method and then delete the method of the object that actually exists on the Panel to solve this problem.

Perhaps everyone will find my solution in the future, or it is wrong. Please criticize the correct. Contact: vipcowrie@sina.com.

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

New Post(0)