Java - 使用 javadoc 和方法注释的约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9863531/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:41:45  来源:igfitidea点击:

Java - Convention for using javadoc along with a method annotation?

javaannotationsjavadoc

提问by Tim

I have the following method:

我有以下方法:

   @Override
   public boolean myMethod()
   {
      // do stuff
   }

If I want to add a javadoc for this method, is there any convention on how to do this along with having the @Override annotation (or any other annotation)?

如果我想为此方法添加一个 javadoc,是否有关于如何执行此操作以及使用 @Override 批注(或任何其他批注)的约定?

The reason I ask is because I've read that javadoc comments MUST be directly before the method (no newlines between the two), and I'm not sure if putting the javadoc comment directly above the @Override annotation would mess things up.

我问的原因是因为我读过 javadoc 注释必须直接在方法之前(两者之间没有换行符),而且我不确定将 javadoc 注释直接放在 @Override 注释上方是否会搞砸。

Would this be the conventional way to do it for instance? Does this work?

例如,这会是传统的做法吗?这行得通吗?

   /**
    * This is my method's javadoc comment.
    */
   @Override
   public boolean myMethod()
   {
      // do stuff
   }

回答by Markus

Yes, this way is the right way, I didn't see yet the other way around. And yes, this way it works. Didn't try the other way around.

是的,这条路是正确的,我还没有看到相反的路。是的,它是这样工作的。没有尝试相反的方法。

   /**
    * This is my method's javadoc comment.
    */
   @Override
   public boolean myMethod()
   {
      // do stuff
   }

But basically I usually wouldn't add a javadoc comment to a method that overrides another method, especially when implementing interfaces. The tag @inheritDocis helpful here, to distribute the documentation without big efforts. But that is specific to this example, you might add other annotations, too.

但基本上我通常不会向覆盖另一个方法的方法添加 javadoc 注释,尤其是在实现接口时。标签@inheritDoc在这里很有用,可以毫不费力地分发文档。但这是特定于本示例的,您也可以添加其他注释。

回答by Tudor

Yes, it will work correctly. For example, in the source code of java.lang.Stringfrom openjdk, they are using javadoc on top of the @Deprecatedannotation.

是的,它会正常工作。例如,在openjdk的 java.lang.String 源代码中,他们在@Deprecated注释之上使用了 javadoc 。