java.io.Serializable 类的意义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5520533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:31:08  来源:igfitidea点击:

What is the significance of java.io.Serializable class?

javaserialization

提问by java_threads

In "Layman's" terms I was wondering if someone could explain to me the significance and general importance of importing the Serializable class to implement the java.io.Serializable interface. asked from java student

在“外行”术语中,我想知道是否有人可以向我解释导入 Serializable 类以实现 java.io.Serializable 接口的重要性和一般重要性。从java学生问

回答by Matt Greer

java.io.Serializableis what is known as a "marker interface". The interface itself has no methods defined in it. So any class can easily implement this interface by simply implementing it:

java.io.Serializable就是所谓的“标记界面”。接口本身没有定义任何方法。所以任何类都可以通过简单地实现它来轻松实现这个接口:

 public class MyClass implements Serializable {
       public void aMethodForMyClass() { }

       // not implementing any specific java.io.Serializable methods because
       // the interface has no methods to implement.
 }

It is a "marker" interface because by implementing this interface you are telling the Java serializer that it is OK to serialize objects of this type. If your class does not implement this interface, the serializer will refuse to serialize any objects of that type.

它是一个“标记”接口,因为通过实现这个接口,你告诉 Java 序列化器可以序列化这种类型的对象。如果您的类没有实现这个接口,序列化器将拒绝序列化该类型的任何对象。

回答by Edwin Dalorzo

All you need to know about Java Serialization can be found in the Java Serialization Specification

您需要了解的有关 Java 序列化的所有信息都可以在Java 序列化规范中找到

The Java Serialization FAQalso contains the answer to your question.

Java序列FAQ也包含了回答你的问题。

Specifically the first question seems to be the same you are asking:

具体来说,第一个问题似乎与您要问的相同:

Why must classes be marked serializable in order to be written to an ObjectOutputStream?

为什么必须将类标记为可序列化才能写入 ObjectOutputStream?

I hope that helps!

我希望这有帮助!