Javadoc 具體使用詳解
很多程序?qū)avadoc都不重視,認(rèn)識(shí)不到Javadoc的作用,很多人都是這樣認(rèn)為的:“我只要寫(xiě)好功能就夠了,寫(xiě)Javadoc太浪費(fèi)時(shí)間,也沒(méi)啥作用,還不如用寫(xiě)Javadoc的時(shí)間再多些個(gè)功能呢!”,我們知道注釋是為了解釋代碼的作用的,是為了將來(lái)給自己或者別人快速了解代碼的,在方法內(nèi)一般用行注釋//的比較多,是針對(duì)一小塊代碼做出解釋的,而Javadoc的作用是針對(duì)整個(gè)方法或者整個(gè)類(lèi)做一個(gè)簡(jiǎn)要的概述的,使得別人不通過(guò)看具體方法代碼就能知道某個(gè)方法或者某個(gè)類(lèi)的作用和功能。寫(xiě)了Javadoc的在別人使用到類(lèi)時(shí),將鼠標(biāo)懸停到類(lèi)上或者方法上,javadoc會(huì)以提示信息顯示出來(lái),這樣開(kāi)發(fā)者在跳進(jìn)源代碼中就能知道類(lèi)或者方法的作用。說(shuō)到這里可能還是有很多人不能認(rèn)同這種觀點(diǎn),還是認(rèn)識(shí)不到Javadoc的作用。我們看一下Spring框架,隨便打開(kāi)一個(gè)文件可以看到一般注釋內(nèi)容都要比代碼量多,有的時(shí)候注釋量占整個(gè)文件內(nèi)容的2/3。有人還是認(rèn)為Spring是大框架,每個(gè)Java項(xiàng)目都在用他們寫(xiě)的好事應(yīng)該的,我們公司自己的項(xiàng)目就我們公司幾個(gè)人看,沒(méi)必要花時(shí)間去寫(xiě)多余的Javadoc,那你是不是該這么認(rèn)為了Spring大廠中的頂尖大牛都這么做,我們小菜鳥(niǎo)是不是對(duì)自己要求更嚴(yán)格一些,向大牛去學(xué)習(xí)呢?!假如在公司A程序員寫(xiě)了Javadoc,B程序員只寫(xiě)功能不寫(xiě)Javadoc不寫(xiě)注釋?zhuān)敲匆话銜?huì)認(rèn)為A程序員會(huì)比B程序員做的好。認(rèn)識(shí)不到Javadoc的作用就像認(rèn)識(shí)不到設(shè)計(jì)模式的作用一樣,很多人都認(rèn)識(shí)不到設(shè)計(jì)模式的作用,認(rèn)為將簡(jiǎn)單問(wèn)題復(fù)雜化,你看一下每個(gè)大框架都會(huì)用到多種設(shè)計(jì)模式,如果只知道寫(xiě)增刪改查,再工作幾年仍然不會(huì)有提高。
一:簡(jiǎn)介
Javadoc用于描述類(lèi)或者方法的作用。Javadoc可以寫(xiě)在類(lèi)上面和方法上面。
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html
二:寫(xiě)在類(lèi)上面的Javadoc
寫(xiě)在類(lèi)上的文檔標(biāo)注一般分為三段:
- 第一段:概要描述,通常用一句或者一段話簡(jiǎn)要描述該類(lèi)的作用,以英文句號(hào)作為結(jié)束
- 第二段:詳細(xì)描述,通常用一段或者多段話來(lái)詳細(xì)描述該類(lèi)的作用,一般每段話都以英文句號(hào)作為結(jié)束
- 第三段:文檔標(biāo)注,用于標(biāo)注作者、創(chuàng)建時(shí)間、參閱類(lèi)等信息
第一段:概要描述
單行示例:
package org.springframework.util; /** * Miscellaneous {@link String} utility methods. * */ public abstract class StringUtils {
多行示例:
package java.lang; /** * Class {@code Object} is the root of the class hierarchy. * Every class has {@code Object} as a superclass. All objects, * including arrays, implement the methods of this class. */ public class Object {}
在注釋中出現(xiàn)以@開(kāi)頭東東被稱之為Javadoc文檔標(biāo)記,是JDK定義好的,如@author、@version、@since、@see、@link、@code、@param、@return、@exception、@throws等。
1. @link:{@link 包名.類(lèi)名#方法名(參數(shù)類(lèi)型)} 用于快速鏈接到相關(guān)代碼
@link的使用語(yǔ)法{@link 包名.類(lèi)名#方法名(參數(shù)類(lèi)型)}
,其中當(dāng)包名在當(dāng)前類(lèi)中已經(jīng)導(dǎo)入了包名可以省略,可以只是一個(gè)類(lèi)名,也可以是僅僅是一個(gè)方法名,也可以是類(lèi)名.方法名,使用此文檔標(biāo)記的類(lèi)或者方法,可用通過(guò)按住Ctrl鍵+單擊 可以快速跳到相應(yīng)的類(lèi)或者方法上,解析成html其實(shí)就是使用< code> 包名.類(lèi)名#方法名(參數(shù)類(lèi)型)< /code>
@link示例
// 完全限定的類(lèi)名 {@link java.lang.Character} // 省略包名 {@link String} // 省略類(lèi)名,表示指向當(dāng)前的某個(gè)方法 {@link #length()} // 包名.類(lèi)名.方法名(參數(shù)類(lèi)型) {@link java.lang.String#charAt(int)}
2. @code: {@code text} 將文本標(biāo)記為code
{@code text} 會(huì)被解析成<code> text </code>
將文本標(biāo)記為代碼樣式的文本,在code內(nèi)部可以使用 < 、> 等不會(huì)被解釋成html標(biāo)簽, code標(biāo)簽有自己的樣式
一般在Javadoc中只要涉及到類(lèi)名或者方法名,都需要使用@code進(jìn)行標(biāo)記。
第二段:詳細(xì)描述
詳細(xì)描述一般用一段或者幾個(gè)鍛煉來(lái)詳細(xì)描述類(lèi)的作用,詳細(xì)描述中可以使用html標(biāo)簽,如<p>、<pre>、<a>、<ul>、<i>
等標(biāo)簽, 通常詳細(xì)描述都以段落p標(biāo)簽開(kāi)始。
詳細(xì)描述和概要描述中間通常有一個(gè)空行來(lái)分割
package org.springframework.util; /** * Miscellaneous {@link String} utility methods. * * <p>Mainly for internal use within the framework; consider * <a rel="external nofollow" rel="external nofollow" >Apache's Commons Lang</a> * for a more comprehensive suite of {@code String} utilities. * * <p>This class delivers some simple functionality that should really be * provided by the core Java {@link String} and {@link StringBuilder} * classes. It also provides easy-to-use methods to convert between * delimited strings, such as CSV strings, and collections and arrays. * */ public abstract class StringUtils {
一般段落都用p標(biāo)簽來(lái)標(biāo)記,凡涉及到類(lèi)名和方法名都用@code標(biāo)記,凡涉及到組織的,一般用a標(biāo)簽提供出來(lái)鏈接地址。
3. @param
一般類(lèi)中支持泛型時(shí)會(huì)通過(guò)@param來(lái)解釋泛型的類(lèi)型
/** * @param <E> the type of elements in this list */ public interface List<E> extends Collection<E> {}
4. @author
詳細(xì)描述后面一般使用@author來(lái)標(biāo)記作者,如果一個(gè)文件有多個(gè)作者來(lái)維護(hù)就標(biāo)記多個(gè)@author,@author 后面可以跟作者姓名(也可以附帶郵箱地址)、組織名稱(也可以附帶組織官網(wǎng)地址)
// 純文本作者 @author Rod Johnson // 純文本作者,郵件 @author Igor Hersht, igorh@ca.ibm.com // 超鏈接郵件 純文本作者 @author <a href="mailto:ovidiu@cup.hp.com" rel="external nofollow" >Ovidiu Predescu</a> // 純文本郵件 @author shane_curcuru@us.ibm.com // 純文本 組織 @author Apache Software Foundation // 超鏈接組織地址 純文本組織 @author <a rel="external nofollow" > Apache Jakarta Turbine</a>
5. @see 另請(qǐng)參閱
@see 一般用于標(biāo)記該類(lèi)相關(guān)聯(lián)的類(lèi),@see即可以用在類(lèi)上,也可以用在方法上。
/** * @see IntStream * @see LongStream * @see DoubleStream * @see <a href="package-summary.html" rel="external nofollow" >java.util.stream</a> * / public interface Stream<T> extends BaseStream<T, Stream<T>> {}
6. @since 從以下版本開(kāi)始
@since 一般用于標(biāo)記文件創(chuàng)建時(shí)項(xiàng)目當(dāng)時(shí)對(duì)應(yīng)的版本,一般后面跟版本號(hào),也可以跟是一個(gè)時(shí)間,表示文件當(dāng)前創(chuàng)建的時(shí)間
package java.util.stream; /** * @since 1.8 */ public interface Stream<T> extends BaseStream<T, Stream<T>> {}
package org.springframework.util; /** * @since 16 April 2001 */ public abstract class StringUtils {}
7. @version 版本
@version 用于標(biāo)記當(dāng)前版本,默認(rèn)為1.0
package com.sun.org.apache.xml.internal.resolver; /** * @version 1.0 */ public class Resolver extends Catalog {}
三:寫(xiě)在方法上的Javadoc
寫(xiě)在方法上的文檔標(biāo)注一般分為三段:
- 第一段:概要描述,通常用一句或者一段話簡(jiǎn)要描述該方法的作用,以英文句號(hào)作為結(jié)束
- 第二段:詳細(xì)描述,通常用一段或者多段話來(lái)詳細(xì)描述該方法的作用,一般每段話都以英文句號(hào)作為結(jié)束
- 第三段:文檔標(biāo)注,用于標(biāo)注參數(shù)、返回值、異常、參閱等
方法詳細(xì)描述上經(jīng)常使用html標(biāo)簽來(lái),通常都以p標(biāo)簽開(kāi)始,而且p標(biāo)簽通常都是單標(biāo)簽,不使用結(jié)束標(biāo)簽,其中使用最多的就是p標(biāo)簽和pre標(biāo)簽,ul標(biāo)簽, i標(biāo)簽。
pre元素可定義預(yù)格式化的文本。被包圍在pre元素中的文本通常會(huì)保留空格和換行符。而文本也會(huì)呈現(xiàn)為等寬字體,pre標(biāo)簽的一個(gè)常見(jiàn)應(yīng)用就是用來(lái)表示計(jì)算機(jī)的源代碼。
一般p經(jīng)常結(jié)合pre使用,或者pre結(jié)合@code共同使用(推薦@code方式)
一般經(jīng)常使用pre來(lái)舉例如何使用方法
注意:pre>標(biāo)簽中如果有小于號(hào)、大于號(hào)、例如泛型 在生產(chǎn)javadoc時(shí)會(huì)報(bào)錯(cuò)
/** * Check whether the given {@code CharSequence} contains actual <em>text</em>. * <p>More specifically, this method returns {@code true} if the * {@code CharSequence} is not {@code null}, its length is greater than * 0, and it contains at least one non-whitespace character. * <p><pre class="code"> * StringUtils.hasText(null) = false * StringUtils.hasText("") = false * StringUtils.hasText(" ") = false * StringUtils.hasText("12345") = true * StringUtils.hasText(" 12345 ") = true * </pre> * @param str the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not {@code null}, * its length is greater than 0, and it does not contain whitespace only * @see Character#isWhitespace */ public static boolean hasText(@Nullable CharSequence str) { return (str != null && str.length() > 0 && containsText(str)); }
{@code Person[] men = people.stream() .filter(p -> p.getGender() == MALE) .toArray(Person[]::new); }
8. @param
@param 后面跟參數(shù)名,再跟參數(shù)描述
/** * @param str the {@code CharSequence} to check (may be {@code null}) */ public static boolean containsWhitespace(@Nullable CharSequence str) {}
9. @return
@return 跟返回值的描述
/** * @return {@code true} if the {@code String} is not {@code null}, its */ public static boolean hasText(@Nullable String str){}
10. @throws
@throws 跟異常類(lèi)型 異常描述 , 用于描述方法內(nèi)部可能拋出的異常
/** * @throws IllegalArgumentException when the given source contains invalid encoded sequences */ public static String uriDecode(String source, Charset charset){}
11. @exception
用于描述方法簽名throws對(duì)應(yīng)的異常
/** * @exception IllegalArgumentException if <code>key</code> is null. */ public static Object get(String key) throws IllegalArgumentException {}
12. @see
@see既可以用來(lái)類(lèi)上也可以用在方法上,表示可以參考的類(lèi)或者方法
/** * @exception IllegalArgumentException if <code>key</code> is null. */ public static Object get(String key) throws IllegalArgumentException {}
13. @value
用于標(biāo)注在常量上,{@value} 用于表示常量的值
/** 默認(rèn)數(shù)量 {@value} */ private static final Integer QUANTITY = 1;
14. @inheritDoc
@inheritDoc用于注解在重寫(xiě)方法或者子類(lèi)上,用于繼承父類(lèi)中的Javadoc
- 基類(lèi)的文檔注釋被繼承到了子類(lèi)
- 子類(lèi)可以再加入自己的注釋?zhuān)ㄌ厥饣瘮U(kuò)展)
- @return @param @throws 也會(huì)被繼承
四:示例
spring-core中的StringUtils 示例
package org.springframework.util; /** * Miscellaneous {@link String} utility methods. * * <p>Mainly for internal use within the framework; consider * <a rel="external nofollow" rel="external nofollow" >Apache's Commons Lang</a> * for a more comprehensive suite of {@code String} utilities. * * <p>This class delivers some simple functionality that should really be * provided by the core Java {@link String} and {@link StringBuilder} * classes. It also provides easy-to-use methods to convert between * delimited strings, such as CSV strings, and collections and arrays. * * @author Rod Johnson * @author Juergen Hoeller * @author Keith Donald * @author Rob Harrop * @author Rick Evans * @author Arjen Poutsma * @author Sam Brannen * @author Brian Clozel * @since 16 April 2001 */ public abstract class StringUtils { /** * Decode the given encoded URI component value. Based on the following rules: * <ul> * <li>Alphanumeric characters {@code "a"} through {@code "z"}, {@code "A"} through {@code "Z"}, * and {@code "0"} through {@code "9"} stay the same.</li> * <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and {@code "*"} stay the same.</li> * <li>A sequence "{@code %<i>xy</i>}" is interpreted as a hexadecimal representation of the character.</li> * </ul> * * @param source the encoded String * @param charset the character set * @return the decoded value * @throws IllegalArgumentException when the given source contains invalid encoded sequences * @since 5.0 * @see java.net.URLDecoder#decode(String, String) */ public static String uriDecode(String source, Charset charset) {}
package com.example.demo; /** * 類(lèi) {@code OrderService} 訂單服務(wù)層. * * <p> 主要包括 創(chuàng)建訂單、取消訂單、查詢訂單等功能更 * * @see Order * @author <a href="mailto:mengday.zhang@gmail.com" rel="external nofollow" >Mengday Zhang</a> * @since 2018/5/12 */ public class OrderService { /** 默認(rèn)數(shù)量 {@value} */ private static final Integer QUANTITY = 1; /** * 創(chuàng)建訂單. * * <p> 創(chuàng)建訂單需要傳用戶id和商品列表(商品id和商品數(shù)量). * * <p><pre>{@code * 演示如何使用該方法 * List<Goods> items = new ArrayList<>(); * Goods goods = new Goods(1L, BigDecimal.ONE); * Goods goods2 = new Goods(2L, BigDecimal.TEN); * items.add(goods); * items.add(goods2); * * Order order1 = new Order(); * order.setUserId("1"); * order.setItems(items); * OrderService#createOrder(order); * } * </pre> * * @param order 訂單信息 * @throws NullPointerException 參數(shù)信息為空 * @exception IllegalArgumentException 數(shù)量不合法 * @return 是否創(chuàng)建成功 * @version 1.0 * @see {@link Order} */ public boolean createOrder(Order order) throws IllegalArgumentException{ Objects.requireNonNull(order); List<Goods> items = order.getItems(); items.forEach(goods -> { BigDecimal quantity = goods.getQuantity(); if (quantity == null || BigDecimal.ZERO.compareTo(quantity) == 0) { throw new IllegalArgumentException(); } }); System.out.println("create order..."); return true; } }
五:生成Javadoc
idea生成javadoc http://www.dbjr.com.cn/article/192943.htm
通過(guò)IDEA生成Javadoc: Tools --> Generate JavaDoc -->
注意要配置編碼,如果不配送為生成亂碼,還需要配置Output directory
到此這篇關(guān)于Javadoc 具體使用詳解的文章就介紹到這了,更多相關(guān)Javadoc 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中double類(lèi)型運(yùn)算結(jié)果異常的解決方法
下面小編就為大家?guī)?lái)一篇java中double類(lèi)型運(yùn)算結(jié)果異常的解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12SpringBoot詳細(xì)講解靜態(tài)資源導(dǎo)入的實(shí)現(xiàn)
在Web開(kāi)發(fā)過(guò)程中,我們需要接觸許多靜態(tài)資源,如CSS、JS、圖片等;在之前的開(kāi)發(fā)中,這些資源都放在Web目錄下,用到的時(shí)候按照對(duì)應(yīng)路徑訪問(wèn)即可。不過(guò)在SpringBoot項(xiàng)目中,沒(méi)有了Web目錄,那這些靜態(tài)資源該放到哪里去,又要如何訪問(wèn)呢?這就是我們要講的靜態(tài)資源導(dǎo)入2022-05-05Java導(dǎo)出oracle表結(jié)構(gòu)實(shí)例詳解
這篇文章主要介紹了 Java導(dǎo)出oracle表結(jié)構(gòu)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03基于Spring Security實(shí)現(xiàn)對(duì)密碼進(jìn)行加密和校驗(yàn)
我們?cè)谌腴T(mén)案例中,其實(shí)已經(jīng)是一個(gè)非常簡(jiǎn)單的認(rèn)證,但是用戶名是寫(xiě)死的,密碼也需要從控制臺(tái)查看,很顯然實(shí)際中并不能這么做,下面的學(xué)習(xí)中,我們來(lái)實(shí)現(xiàn)基于內(nèi)存模型的認(rèn)證以及用戶的自定義認(rèn)證,密碼加密等內(nèi)容,需要的朋友可以參考下2024-07-07使用java實(shí)現(xiàn)“釘釘微應(yīng)用免登進(jìn)入某H5系統(tǒng)首頁(yè)“功能”
這篇文章主要介紹了用java實(shí)現(xiàn)“釘釘微應(yīng)用,免登進(jìn)入某H5系統(tǒng)首頁(yè)“功能”,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式
這篇文章主要介紹了Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09