57

我需要包含*/在我的 JavaDoc 注释中。问题是这也是关闭评论的相同顺序。引用/逃避这个的正确方法是什么?

例子:

/**
 * Returns true if the specified string contains "*/".
 */
public boolean containsSpecialSequence(String str)

跟进:看来我可以使用/斜线。唯一的缺点是直接在文本编辑器中查看代码时,这并不是那么可读。

/**
 * Returns true if the specified string contains "*/".
 */
4

6 回答 6

44

使用 HTML 转义。

所以在你的例子中:

/**
 * Returns true if the specified string contains "*/".
 */
public boolean containsSpecialSequence(String str)

/转义为“/”字符。

Javadoc 应该将不受干扰的转义序列插入到它生成的 HTML 中,并且应该在浏览器中呈现为“*/”。

如果你想非常小心,你可以转义两个字符:*/转换为*/

编辑:

跟进:看来我可以使用 / 为斜线。唯一的缺点是,当直接查看代码时,这并不是那么可读。

所以?重点不是让您的代码可读,而是让您的代码文档可读。大多数 Javadoc 注释都嵌入了复杂的 HTML 以进行解释。地狱,C# 的等价物提供了一个完整的 XML 标记库。我在那里看到了一些非常复杂的结构,让我告诉你。

编辑 2: 如果它太困扰您,您可能会嵌入一个解释编码的非 javadoc 内联注释:

/**
 * Returns true if the specified string contains "*/".
 */
// returns true if the specified string contains "*/"
public boolean containsSpecialSequence(String str)
于 2009-03-10T19:15:04.497 回答
13

没有人提到{@literal}。这是另一种方法:

/**
 * Returns true if the specified string contains "*{@literal /}".
 */

不幸的是,您一次无法逃脱*/。有一些缺点,这也修复了:

唯一的缺点是直接在文本编辑器中查看代码时,这并不是那么可读。

于 2014-07-11T10:52:51.663 回答
9
/**
 * Returns true if the specified string contains "*/".
 */

这是“正确”的解决方案,但为了便于阅读,我可能会选择:

/**
 * Returns true if the string contains an asterisk followed by slash.
 */
于 2009-03-10T21:03:15.690 回答
6

使用实体

*/ 

在您的文档中,它将显示为“*/”

于 2009-03-10T19:15:27.273 回答
6

我偶然发现的另一种方式,只是为了完整性:添加一些不会改变 * 和 / 之间的输出的 HTML 标记。

  /**
   * *<b/>/
   */

与 HTML 转义解决方案相比,这似乎是一种丑陋的 hack,但它也能在 HTML 输出中产生正确的结果。

于 2009-03-10T19:39:35.640 回答
6

我建议您在附近的某处添加一行评论,例如

// *&#47; is html for */
于 2009-03-10T19:44:32.447 回答