我如何使用“<”和“>”在javadoc中没有格式化?

发布于 2024-09-03 10:27:39 字数 118 浏览 5 评论 0 原文

如果我在 javadoc 中编写 ,它不会出现,因为标签在格式化文本方面具有特殊功能。

如何在 javadoc 中显示这些字符?

If I write <xmlElement> in a javadoc, it does not appear, because tags have special functions on formatting texts.

How can I show this chars in a javadoc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

自由如风 2024-09-10 10:27:40

一个完整的示例展示了使用 {@code }{@literal } 以及转义来保留 <> 。

package example;

public class Example {

    /**
     * Prints the perfect XML.
     * 
     * The following XML will be printed:
     * 
     *
     * <pre>{@code
<hello>
    <world>
        <value>42</value>
        <value>
           * oops?
        </value>
    </world>
</hello>
     * }</pre>
     * 
     * 
     * Keep leading stars if you want. Will still be ignored.
     * 
     * <pre>{@code
     * <hello>
     *     <world>
     *         <value>42</value>
     *         <value>
     *            * oops?
     *         </value>
     *     </world>
     * </hello>
     * }</pre>
     * 
     * Look at stdout.
     */
    public void printXML() {
    }

    /**
     * Prints meanings of {@code Map<String, Integer>}.
     * 
     * {@code Map<String, Integer>} can represent:
     * <ul>
     * <li>{@literal Map<Input, Hash>}</li>
     * <li>{@literal Map<Team, Score>}</li>
     * <li>{@literal Map<Country, GDP>}</li>
     * </ul>
     * 
     * Look at stdout.
     */
    public void printMapRelations() {
    }

    /**
     * {@literal 
     * Proves 4 < 2 + 3 is the same as 2 + 3 > 4
     * 
     * No markup is required to show that flipping the left and right sides 
     * of an < expression causes < to become >.
     * 
     * Don't use <and> carelessly.
     * }
     */
    public void printProof() {
    }

    /**
     * Javadoc is HTML so encoded <strong><</strong> and <em>></em> work but
     * not super
     * legible in source.
     */
    public void printOther() {
    }
}

注意:IDE 预览和实际输出有时看起来略有不同。

提醒:./gradlew javadoc 默认情况下会构建到 build/docs/javadoc/

另请参阅:

A full example showing off uses of {@code }, {@literal }, and escapes to preserve < and >.

package example;

public class Example {

    /**
     * Prints the perfect XML.
     * 
     * The following XML will be printed:
     * 
     *
     * <pre>{@code
<hello>
    <world>
        <value>42</value>
        <value>
           * oops?
        </value>
    </world>
</hello>
     * }</pre>
     * 
     * 
     * Keep leading stars if you want. Will still be ignored.
     * 
     * <pre>{@code
     * <hello>
     *     <world>
     *         <value>42</value>
     *         <value>
     *            * oops?
     *         </value>
     *     </world>
     * </hello>
     * }</pre>
     * 
     * Look at stdout.
     */
    public void printXML() {
    }

    /**
     * Prints meanings of {@code Map<String, Integer>}.
     * 
     * {@code Map<String, Integer>} can represent:
     * <ul>
     * <li>{@literal Map<Input, Hash>}</li>
     * <li>{@literal Map<Team, Score>}</li>
     * <li>{@literal Map<Country, GDP>}</li>
     * </ul>
     * 
     * Look at stdout.
     */
    public void printMapRelations() {
    }

    /**
     * {@literal 
     * Proves 4 < 2 + 3 is the same as 2 + 3 > 4
     * 
     * No markup is required to show that flipping the left and right sides 
     * of an < expression causes < to become >.
     * 
     * Don't use <and> carelessly.
     * }
     */
    public void printProof() {
    }

    /**
     * Javadoc is HTML so encoded <strong><</strong> and <em>></em> work but
     * not super
     * legible in source.
     */
    public void printOther() {
    }
}

Note: The IDE preview and the actual output can sometiems look slightly different.

Reminder: ./gradlew javadoc will by default build to build/docs/javadoc/.

See also:

半夏半凉 2024-09-10 10:27:39

您可以使用 < 表示 < ,使用 > 表示 >

You can use < for < and > for > .

月牙弯弯 2024-09-10 10:27:39

最新版本的 JavaDoc 支持 {@literal AC};这会正确输出内容(转义生成的 HTML 中的“<”和“>”)。

请参阅http://download.oracle.com/javase/1.5 .0/docs/guide/javadoc/whatsnew-1.5.0.html

Recent versions of JavaDoc support {@literal A<B>C}; this outputs the content correctly (escaping the '<' and '>' in the generated HTML).

See http://download.oracle.com/javase/1.5.0/docs/guide/javadoc/whatsnew-1.5.0.html

策马西风 2024-09-10 10:27:39

考虑到 XML 是实际代码,我相信 Javadoc 中的 XML 片段更适合 {@code AC} 标记,而不是 {@literal AC} 标记。

{@code } 标签使用固定宽度的字体,使其内容与实际代码一样突出。

Considering XML is actual code, I believe XML snippets in Javadoc are better suited for the {@code A<B>C} tag rather than the {@literal A<B>C} tag.

The {@code } tag uses a fixed-width font which makes its content standout as actual code.

So要识趣 2024-09-10 10:27:39

将它们转义为 HTML:<>

Escape them as HTML: < and >

野稚 2024-09-10 10:27:39

插入

 {@code} 在 javadocs 中保存尖括号和空行,并且被广泛使用,例如,参见 java.util.Stream。

<pre>{@code
   A<B>C

   D<E>F
}</pre>

Interposition of <pre> and {@code} saves angle brackets and empty lines in javadocs and is widely used, see java.util.Stream for example.

<pre>{@code
   A<B>C

   D<E>F
}</pre>
征﹌骨岁月お 2024-09-10 10:27:39

您只需使用尖括号之一的 HTML 等效项即可。 < 可以表示为 <<。这是取自真实 Javadoc 的示例:

<pre>
<complexType>
  <complexContent>
    <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
      <sequence>
      [...]

显示为:

<complexType>
   <complexContent>
     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
       <sequence>

You only need to use the HTML equivalent for one of the angle brackets. The < can be represented as either < or <. Here's a sample taken from real Javadoc:

<pre>
<complexType>
  <complexContent>
    <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
      <sequence>
      [...]

This displays as:

<complexType>
   <complexContent>
     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
       <sequence>
嘴硬脾气大 2024-09-10 10:27:39

只需用 {@code} 包围它,如下所示:

{@code <xmlElement>}

Just surround it with {@code} like this:

{@code <xmlElement>}
深海夜未眠 2024-09-10 10:27:39

如果你设置maven使用markdown,你可以用反引号将其括起来。

`AC` 读起来比 {@code AC} 好一点

If you set maven up to use markdown, you can just surround it with backticks.

`A<B>C` reads a bit nicer than {@code A<B>C}

幻想少年梦 2024-09-10 10:27:39

就我而言,我想放入 javadocs List...

我通过提供指向我的 SomeClass 的链接添加了更具体的信息,所以这里是我的解决方案:

List<{@link SomeClass}>

这导致了一个干净的结果:

List<SomeClass>

带有下划线的“SomeClass”指向指定的类。

(当然,如果SomeClass不在同一个包中,则应引用完整路径)

In my case where I wanted to put in my javadocs List<SomeClass>...

I added an even more specific information by giving the link to my SomeClass, so here is my solution :

List<{@link SomeClass}>

Which resulted to a clean :

List<SomeClass>

With underlined 'SomeClass' directing to the specified class.

(of course if the SomeClass is not in same package, the complete path should be referenced)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文