Java 可序列化和瞬态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1922897/
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
Serializable and transient
提问by andrii
To make a class serializable we do the following:
为了使类可序列化,我们执行以下操作:
class A implements Serializable {
transient Object a;
}
And not this:
而不是这个:
serializable class A {
transient Object a;
}
Why, if we want to make a class serializable, do we implement a special interface. And if we want to exclude some fields we use the keywordtransient
?
Why aren't special keywords used in both cases? I mean were there any reasons to make the same thing in different ways? I know, there is no such keyword as serializable
but why wasn't it introduced instead of the special interface Serializable
?
为什么,如果我们想让一个类可序列化,我们要实现一个特殊的接口。如果我们想排除某些字段,我们使用关键字transient
? 为什么在这两种情况下都不使用特殊关键字?我的意思是有什么理由以不同的方式制作相同的东西吗?我知道,没有这样的关键字 asserializable
但为什么不引入它而不是特殊接口Serializable
?
采纳答案by Michael Borgwardt
Why isn't used some special keyword to mark classes as serializable too? Serializable interface looks like a magic numbers in code and not like the language feature.
为什么不使用一些特殊关键字来将类标记为可序列化?Serializable 接口在代码中看起来像一个神奇的数字,而不是语言功能。
I think you have to look at it the other way: language keywords exist mainly to support compile-time language constructs. Serialization is a runtime mechanism. Additionally, you don't want to have an extra keyword for everything, because you then can't use it as an identifier. A marker interface on the other hand is much less intrusive.
我认为您必须换一种方式看待它:语言关键字的存在主要是为了支持编译时语言结构。序列化是一种运行时机制。此外,您不希望为所有内容都添加一个额外的关键字,因为这样您就无法将其用作标识符。另一方面,标记界面的侵入性要小得多。
The question is thus: why do we need a language keyword to mark transient fields? And the answer is that there simply was no other way to mark specific fields at that time.
问题是:为什么我们需要语言关键字来标记瞬态字段?答案是当时根本没有其他方法可以标记特定字段。
Nowadays, one would use annotations for this purpose in both cases (and for other things like the obscure strictfp
keyword as well).
如今,在这两种情况下(以及其他诸如晦涩的strictfp
关键字之类的东西),人们都会为此目的使用注释。
回答by T.J. Crowder
Serializable
is a marker interface. Interfaces are a standard way (in Java and in some other languages) of indicating features of a class; an "is a" relaionship. Making Serializable
an interface means we can declare methods that accept or return Serializable
s just like we can methods that work with other interfaces. Anything else would have required syntax changes to the language (at the time; now we have annotations, but I think an interface would still be used).
Serializable
是一个标记界面。接口是指示类特征的标准方式(在 Java 和其他一些语言中);一个“是”关系。制作Serializable
接口意味着我们可以声明接受或返回Serializable
s 的方法,就像我们可以使用其他接口的方法一样。其他任何事情都需要对语言进行语法更改(当时;现在我们有了注释,但我认为仍然会使用接口)。
回答by spork
So you're asking why you can't mark a class as not serializable (like a transient member)? Why wouldn't you just not mark class members of the not-to-serialize type as transient? Or use a serialization delegate for that class type when you do the serialization? It seems a little weird that you would want to tell Java to notdo something at this level instead of telling it todo something.
所以你问为什么不能将类标记为不可序列化(如瞬态成员)?为什么不将非序列化类型的类成员标记为瞬态?或者在进行序列化时使用该类类型的序列化委托?这似乎有点不可思议,你想告诉Java没有在这个级别做一些事情,而不是告诉它来做些什么。
回答by rsp
Serializable
is a marker interface (like Cloneable
) that is used to set a flag for standard Java runtime library code that an object can be serialised according to the designer of that class.
Serializable
是一个标记接口(如Cloneable
),用于为标准 Java 运行时库代码设置标志,该标志可以根据该类的设计者对对象进行序列化。
The transient
keyword can be used to specify that an attribute does not need to be serialised, for instance because it is a derived attribute.
该transient
关键字可用于指定的属性不需要被序列化,例如,因为它是一个派生属性。
See also this reply to a similar question on SOand this one about designing marker interfaces.
另请参阅对 SO 上类似问题的回复以及有关设计标记界面的回复。
Update
更新
Why marker interfaces and no keywords for things like serializable, cloneable, etc? My guess would be the possibility to consistently extend the Java runtime lib with new marker interfaces combined with too many keywords if behavioural aspects made it into the language.
为什么标记接口而没有诸如可序列化、可克隆等之类的关键字?我的猜测是,如果行为方面将其纳入语言,则可以使用新的标记接口和过多的关键字一致地扩展 Java 运行时库。
The fact that class attributes cannot implement Interfaces and transient
can be seen as a generic property of an attribute makes sense of introducing transient
as a language keyword.
类属性不能实现接口并且transient
可以被视为属性的通用属性这一事实使得transient
作为语言关键字引入是有意义的。
回答by Hitesh Garg
Transient keywords are used to protect a variable or a field from being stored and we do this to protect some sensitive information we just don't want to distribute at every place and we use Serializable interface to make a class Serializable. Although we can use Externalizable interface also but we prefer to use Serializable because of some advantages.
瞬态关键字用于保护变量或字段不被存储,我们这样做是为了保护一些我们不想在每个地方分发的敏感信息,我们使用 Serializable 接口来创建一个 Serializable 类。虽然我们也可以使用 Externalizable 接口,但我们更喜欢使用 Serializable 因为一些优点。
Go though this to clearly understand Serialization and transient keyword. http://www.codingeek.com/java/io/object-streams-serialization-deserialization-java-example-serializable-interface/
通过这个来清楚地理解序列化和瞬态关键字。 http://www.codingeek.com/java/io/object-streams-serialization-deserialization-java-example-serializable-interface/