简体   繁体   English

我应该记录重写的hashCode()和toString()吗?

[英]Should I document overriden hashCode() and toString()?

Is it desirable to document the overriden hashCode() and toString() or similar methods in Java? 是否需要记录Java中重写的hashCode()toString()或类似方法? (not including equals() and similar of course) What are the conventions? (不包括equals()和类似的东西)有哪些约定?

From Oracle's JavaDoc Guide: Oracle的JavaDoc指南中:

So if the documentation of the overridden or implemented method is sufficient, you do not need to add documentation for m() . 因此,如果覆盖或实现的方法的文档足够,则无需为m()添加文档。

If your hashCode or toString are doing something different to the documentation in Object then you need to amend the code rather than the documentation. 如果您的hashCode或toString在做与Object中文档不同的操作,那么您需要修改代码而不是文档。 As anyone using these methods without access to the JavaDoc will expect them to behave as documented in Object. 任何使用这些方法但无法访问JavaDoc的人都希望它们的行为与Object中记录的一样。

JavaDoc: JavaDoc的:

hashCode toString hashCode toString

如果您使用一些需要记录的特殊算法,我将编写内联注释// ...,但这对于依赖于继承的Javadoc表示的合同的API用户来说应该无关紧要,所以我不会将其写为Javadoc注释。

Sometimes it is useful to document them. 有时记录它们很有用。 For instance, if an object is intended to be a value returned by a Swing model (ListModel, TableModel, etc.), its toString method may return a display string intended to be displayed to the user, rather than a string meant for debugging. 例如,如果一个对象旨在成为Swing模型(ListModel,TableModel等)返回的值,则其toString方法可以返回旨在向用户显示的显示字符串,而不是用于调试的字符串。 I often like to make it clear whether an object's toString is suitable for display to an end user. 我经常想弄清楚对象的toString是否适合显示给最终用户。

I will document equals to indicate which properties or what state constitutes equality, like URI.equals and List.equals do. 我将用equals表示equals以指示哪些属性或哪个状态构成了相等性,就像URI.equalsList.equals一样。 Similarly, it is useful to point out the properties or state on which hashCode relies, especially for mutable objects, since it allows a developer to avoid changing that state when the object is used as a hash key. 类似地,指出在其上的属性或状态有用hashCode依赖,尤其是对于可变对象,因为它允许开发人员避免更改状态下,当对象被用作散列密钥。

When there is a reason to have a specified algorithm for computing the hash-code, you should document that algorithm for the hashCode() method. 如果有必要使用指定的算法来计算哈希码,则应为hashCode()方法记录该算法。 Java itself does this for String.hashCode() : Java本身为String.hashCode()执行此操作:

a hash code for this string. 此字符串的哈希码。 The hash code for a String object is computed as 字符串对象的哈希码计算为

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 

using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. 使用int算术,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示幂。 (The hash value of the empty string is zero.) (空字符串的哈希值为零。)

For most other cases, however, the general contract of Object.hashCode() requiring it to be consistent with equals() is enough documentation. 但是,对于大多数其他情况, Object.hashCode()的常规协定要求该协定与equals()保持一致就足够了。

Bloch in Effective Java recommends documenting the format of the toString() message for value classes . Effective Java中的 Bloch建议记录值类toString()消息格式。 He also notes that in that case you should provide a also provide a parsing function (a static factory method) that creates an object of the class given its String representation.. 他还指出,在这种情况下,您还应该提供一个解析函数(一个静态工厂方法),该函数创建给定其String表示形式的类的对象。

You shouldn't override the Object class's documentation of these methods, since it describes the contract that all implementations (including yours) can be expected to follow. 您不应覆盖这些方法的Object类文档,因为它描述了所有实现(包括您的实现)都应遵循的约定。 It may be appropriate to extend the inherited documentation with additional notes about your implementation. 在继承的文档中添加有关实现的其他说明, 可能是合适的。 You can do that like this: 您可以这样做:

/**
 * {@inheritDoc}
 * <p>
 * In this implementation...
 */

The Javadoc tool will insert the superclass documentation in place of the {@inheritDoc} tag, so that the output will consist of that superclass documentation followed by your additional notes. Javadoc工具将在{@inheritDoc}标记的位置插入超类文档,以便输出将由该超类文档和其他注释组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM