我有标题中给出的警告信息。我想理解并移除它。我已经找到了这个问题的一些答案,但我不明白这些答案,因为有太多的专业术语。有可能用简单的语言来解释这个问题吗?
附注:我知道OOP是什么。我知道什么是对象,类,方法,字段和实例化。
P.P.S.如果有人需要我的代码,它在这里:
import java.awt.*;
import javax.swing.*;
public class HelloWorldSwing extends JFrame {
JTextArea m_resultArea = new JTextArea(6, 30);
//====================================================== constructor
public HelloWorldSwing() {
//... Set initial text, scrolling, and border.
m_resultArea.setText("Enter more text to see scrollbars");
JScrollPane scrollingArea = new JScrollPane(m_resultArea);
scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));
// Get the content pane, set layout, add to center
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.pack();
}
public static void createAndViewJFrame() {
JFrame win = new HelloWorldSwing();
win.setTitle("TextAreaDemo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
}
//============================================================= main
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndViewJFrame();
}
});
}
}
来自javadoc:
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:
你可以配置你的IDE:
忽略这一点,而不是给出警告。
自动生成id
根据你的附加问题“讨论的警告消息是我的GUI应用程序冻结的原因吗?”:
不,不可能。只有当您序列化对象并在类改变的不同位置(或时间)反序列化它们时,才会导致问题,并且它不会导致冻结,而是在InvalidClassException中。
到目前为止,其他答案包含大量技术信息。我将尽量按要求用简单的语言回答。
序列化是你对一个对象的实例所做的事情,如果你想将它转储到原始缓冲区,保存到磁盘,以二进制流传输它(例如,通过网络套接字发送一个对象),或者以其他方式创建一个对象的序列化二进制表示。(有关序列化的更多信息,请参阅Wikipedia上的Java序列化)。
如果您不打算序列化您的类,您可以在类@SuppressWarnings(“serial”)上方添加注释。
如果您打算序列化,那么您就有很多事情要担心,这些事情都围绕着UUID的正确使用。基本上,UUID是一种对要序列化的对象进行“版本化”的方法,这样无论哪个进程正在反序列化,都知道它正在正确地反序列化。我将查看为序列化对象确保适当的版本控制以获得更多信息。
它必须在任何时候被改变
影响序列化的更改
(额外的字段,删除的字段,
更改场地顺序,…)
That's not correct, and you will be unable to cite an authoriitative source for that claim. It should be changed whenever you make a change that is incompatible under the rules given in the Versioning of Serializable Objects section of the Object Serialization Specification, which specifically does not include additional fields or change of field order, and when you haven't provided readObject(), writeObject(), and/or readResolve() or /writeReplace() methods and/or a serializableFields declaration that could cope with the change.
来自javadoc:
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:
你可以配置你的IDE:
忽略这一点,而不是给出警告。
自动生成id
根据你的附加问题“讨论的警告消息是我的GUI应用程序冻结的原因吗?”:
不,不可能。只有当您序列化对象并在类改变的不同位置(或时间)反序列化它们时,才会导致问题,并且它不会导致冻结,而是在InvalidClassException中。