java JavaDoc 中带有注释的代码示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2636086/
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
Code example with annotation in JavaDoc
提问by John
my JavaDoc doesn't work when I have a code example with an annotation.
当我有一个带注释的代码示例时,我的 JavaDoc 不起作用。
Any suggestions?
有什么建议?
/**
* <pre>
* public class Demo {
* @DemoAnnotation
* public void demoMethod() {
* }
* }
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DemoAnnotation {
采纳答案by Espen
You must replace @with @in your JavaDoc.
您必须在 JavaDoc 中替换@为@。
回答by Joe Coder
A more general solution: {@literal @}
一个更通用的解决方案: {@literal @}
The
{@literal}tag denotes literal text. The enclosed text is interpreted as not containing HTML markup or nested javadoc tags. For example, the doc comment text:{@literal a<B>c}displays in the generated HTML page unchanged:a<B>c-- that is, the<B>is not interpreted as bold.
该
{@literal}标签是指文字文本。封闭的文本被解释为不包含 HTML 标记或嵌套的 javadoc 标签。例如,文档注释文本:{@literal a<B>c}在生成的 HTML 页面中显示不变:a<B>c--即<B>不被解释为粗体。
Requires Java 5+
需要 Java 5+
回答by avogt
use <code> like this:
像这样使用 <code>:
/**
* <pre><code>
* public class Demo {
* @DemoAnnotation
* public void demoMethod() {
* }
* }
* </code></pre>
*/
produces a paragraph while alone can also be used inline.
单独生成一个段落也可以内联使用。
回答by Gary
You can also use @code to escape the annotation, but you must do each one individually like this:
您也可以使用@code 来转义注释,但您必须像这样单独执行每个注释:
/**
* <pre>
* public class Demo {
* {@code @DemoAnnotation }
* {@code @AnotherAnnotation }
* public void demoMethod() {
* }
* }
* </pre>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DemoAnnotation {
will render like this:
将呈现如下:
public class Demo {
@DemoAnnotation
@AnotherAnnotation
public void demoMethod() {
}
}
Note: It will not work to simply wrap both annotations - or the entire code sample - in one @code block.
注意:将两个注释或整个代码示例简单地包装在一个 @code 块中是行不通的。
回答by nagaraju k
You must use the @Documented annotation for adding annotations in the javadoc. Check the implementation on the API
您必须使用 @Documented 批注在 javadoc 中添加批注。检查 API 上的实现

