将字段标记为瞬态如何使序列化对象成为可能

发布于 2024-10-20 05:04:08 字数 550 浏览 4 评论 0原文

public class Foo implements java.io.Serializable {
   private int v1;
   private static double v2;
   private Loan v3 = new Loan();
}

选项:
A. Foo 的实例可以序列化,因为 Foo 实现了 Serialized。
B. Foo 的实例无法序列化,因为 Foo 包含不可序列化的实例变量 v3。
C. 如果将 v3 标记为瞬态,则 Foo 的实例是可序列化的。
D. b 和 c

答案:D

解释:即使对象的类实现了 java.io.Serialized,也可能无法序列化,因为它可能包含不可序列化的实例变量。

现在我的问题是:

据我所知,使用了瞬态 关闭序列化。那么在这种情况下瞬态如何帮助我们序列化 foo 呢?

public class Foo implements java.io.Serializable {
   private int v1;
   private static double v2;
   private Loan v3 = new Loan();
}

Options:
A. An instance of Foo can be serialized because Foo implements Serializable.
B. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3.
C. If you mark v3 as transient, an instance of Foo is serializable.
D. b and c

Answer: D

Explanation: An object may not be serialized even though its class implements java.io.Serializable, because it may contain non-serializable instance variables.

Now my question is:

As far as I know, transient is used to turn off serialization. Then how is transient in this case, helping us to serialize foo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2024-10-27 05:04:08

transient 不会完全禁用序列化;它只是标记不会被序列化的成员。它通常用于对象未序列化时不正确或不相关的内容,或者存储不太安全的内容(密码、解密的数据等),或者可能会导致无法序列化的内容。很容易重建。

在这种情况下,我假设 Loan 类不可序列化。 (如果是,那么 A 就是正确的。)将 v3 标记为瞬态只是告诉 Java 不要担心该字段,而是继续序列化其他字段。这意味着未序列化的 Foo 可能具有 null v3。如果您还想存储 Loan ,则需要跟踪足够的信息以便随意重新创建它,或者更改类 Loan 以便它实现 < code>java.io.Serialized 也是如此。

或者,如果您需要控制序列化,则可以实现一些方法(writeObjectreadObject)。但这可能有点麻烦。

transient doesn't disable serialization altogether; it just marks members that won't be serialized. It's typically used for stuff that would be incorrect or irrelevant when the object is unserialized, or stuff that it'd be less-than-safe to store (passwords, decrypted data, that sort of thing), or non-serializable stuff that could be easily reconstructed.

In this case, i assume the Loan class isn't serializable. (If it were, then A would be correct.) Marking v3 as transient just tells Java not to worry about that field, but go ahead and serialize the others. This means an unserialized Foo might have a null v3. If you want to store the Loan as well, you'd need to either keep track of enough info to recreate it at will, or change class Loan so that it implements java.io.Serializable as well.

Alternatively, there are methods you could implement (writeObject, readObject) if you need control over serialization. But that can be a bit of a hassle.

拧巴小姐 2024-10-27 05:04:08

那么在这种情况下如何是瞬态的,
帮助我们序列化 foo?

因为它允许您序列化 Foo 的其余部分,以及可序列化的其他成员。

Then how is transient in this case,
helping us to serialize foo?

Because it allows you to serialize the rest of Foo, the other members that are serializable.

花开雨落又逢春i 2024-10-27 05:04:08

首先,您确定 Loan 不可序列化吗?如果是,则 B 不适用。

另一方面,如果确实不是,那么 B 和 C 都是对的。 transient 通常不会关闭序列化,但仅针对与其关联的字段。因此,序列化 Foo 不会传输 v3 字段的值。在接收端,当重新初始化 Foo 实例时,序列化机制不会尝试从流中读取该字段,并将其保留为空。

First, are you sure that Loan is not serializable? If it is, then B doesn't apply.

On the other hand, if it indeed is not, then B and C are right. transient doesn't turn off serialization generally, but only for the field with which it's associated. Thus, serializing Foo would not transmit a value for the v3 field. On the receiving end, when re-initializing the Foo instance, the serialization mechanism wouldn't try to read that field from the stream, and would leave it null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文