简体   繁体   English

关于Java中重写方法的评论

[英]Comments on Overridden method in Java

Should we comment the overridden method or not? 我们应该评论被覆盖的方法吗? If yes, then whether the comment will be a Java Doc or simple comment? 如果是,那么评论是Java Doc还是简单评论?

@SimonC's answer explains how the javadoc utility generates "inherited" documentation for overridden methods. @ SimonC的答案解释了javadoc实用程序如何为重写方法生成“继承”文档。

You can also put explicit javadocs in an override method and they will take precedence over the inherited javadocs. 您还可以在显式方法中放置显式的javadoc,它们将优先于继承的javadoc。 Furthermore, if you put the {@inheritDoc} tag in the override method's explicit javadocs, the inherited comments will be included at that point. 此外,如果将{@inheritDoc}标记放在override方法的显式javadoc中,那么将包含继承的注释。

To answer this: 要回答这个问题:

Should we comment the overridden method or not? 我们应该评论被覆盖的方法吗? If yes, then whether the comment will be a Java Doc or simple comment? 如果是,那么评论是Java Doc还是简单评论?

In my opinion, if the override method refines the documented semantics (contract) of the overridden method (or ... heaven forbid ... breaks the contract), then this deserves to be documented in the override method's javadocs. 在我看来,如果覆盖方法改进了被覆盖方法的文档语义(契约)(或者......天堂禁止......违反合同),那么这应该记录在覆盖方法的javadoc中。 However, if the differences are merely "implementation details", then simple comments (or no comments) are more appropriate. 但是,如果差异仅仅是“实施细节”,那么简单的评论(或没有评论)更合适。

(However, the practice of including a "non-javadoc" comment that refers the reader back to the overridden method's javadoc is, IMO, a waste of screen real-estate ... when I am reading the source code.) (但是,当我阅读源代码时,包含一个“非javadoc”注释,将读者引回到被覆盖的方法的javadoc的做法,IMO,浪费屏幕空间......)

From How to Write Doc Comments for the Javadoc Tool : 如何编写Javadoc工具的Doc注释

Automatic re-use of method comments 自动重复使用方法注释

You can avoid re-typing doc comments by being aware of how the Javadoc tool duplicates (inherits) comments for methods that override or implement other methods. 您可以通过了解Javadoc工具如何复制(继承)覆盖或实现其他方法的方法的注释来避免重新键入文档注释。 This occurs in three cases: When a method in a class overrides a method in a superclass When a method in an interface overrides a method in a superinterface When a method in a class implements a method in an interface In the first two cases, if a method m() overrides another method, The Javadoc tool will generate a subheading "Overrides" in the documentation for m(), with a link to the method it is overriding. 这种情况在三种情况下发生:当类中的方法覆盖超类中的方法时当接口中的方法覆盖超接口中的方法时类中的方法在接口中实现方法在前两种情况下,如果方法m()重写另一个方法,Javadoc工具将在m()的文档中生成一个子标题“Overrides”,其中包含指向它所覆盖的方法的链接。

In the third case, if a method m() in a given class implements a method in an interface, the Javadoc tool will generate a subheading "Specified by" in the documentation for m(), with a link to the method it is implementing. 在第三种情况下,如果给定类中的方法m()在接口中实现方法,则Javadoc工具将在m()的文档中生成子标题“Specified by”,并带有指向它正在实现的方法的链接。

In all three of these cases, if the method m() contains no doc comments or tags, the Javadoc tool will also copy the text of the method it is overriding or implementing to the generated documentation for m(). 在所有这三种情况下,如果方法m()不包含文档注释或标记,则Javadoc工具还将复制它覆盖或实现的方法文本到m()的生成文档。 So if the documentation of the overridden or implemented method is sufficient, you do not need to add documentation for m(). 因此,如果重写或实现的方法的文档已足够,则无需为m()添加文档。 If you add any documentation comment or tag to m(), the "Overrides" or "Specified by" subheading and link will still appear, but no text will be copied. 如果向m()添加任何文档注释或标记,则仍会显示“覆盖”或“指定者”子标题和链接,但不会复制任何文本。

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

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