gpt4 book ai didi

java - SerialVersionUID JavaDoc?

转载 作者:行者123 更新时间:2023-11-30 08:31:56 24 4
gpt4 key购买 nike

在我的 Java 程序中添加文档时,我意识到大多数类都需要声明一个 serialVersionUID 常量属性。我究竟应该如何记录这个属性?如果我使用默认与生成的串行版本 UID,我是否会以不同的方式记录它?

/**
* What goes here? "A unique serial version identifier"
*/
private static final long serialVersionUID = -8922096951749901688L;

最佳答案

首先,serialVersionUID您提供的评论似乎是正确的但不必要

serialVersionUID需要作为 Serializable 的一部分对象(不是所有 java 类),并在此对象的序列化/反序列化期间使用。

作为一般规则。 始终检查 API
在这种情况下,Serializable API,在底部!!

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:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

So you're saying that it should actually not be documented whatsoever because there is javadoc for it already? When I omit the javadoc and mouseover it, it does not show anything :/

它是 Serializable 的一部分接口(interface),所以恕我直言是没有必要的......无论如何,你可以使用自己的评论+ @see注解。像这样的东西(不确定是否可行,现在无法创建 javadoc...)

/**
* A unique serial version identifier
* @see Serializable#serialVersionUID
*/

关于java - SerialVersionUID JavaDoc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com