Javadoc 具體使用詳解
很多程序?qū)avadoc都不重視,認(rèn)識(shí)不到Javadoc的作用,很多人都是這樣認(rèn)為的:“我只要寫好功能就夠了,寫Javadoc太浪費(fèi)時(shí)間,也沒啥作用,還不如用寫Javadoc的時(shí)間再多些個(gè)功能呢!”,我們知道注釋是為了解釋代碼的作用的,是為了將來給自己或者別人快速了解代碼的,在方法內(nèi)一般用行注釋//的比較多,是針對(duì)一小塊代碼做出解釋的,而Javadoc的作用是針對(duì)整個(gè)方法或者整個(gè)類做一個(gè)簡要的概述的,使得別人不通過看具體方法代碼就能知道某個(gè)方法或者某個(gè)類的作用和功能。寫了Javadoc的在別人使用到類時(shí),將鼠標(biāo)懸停到類上或者方法上,javadoc會(huì)以提示信息顯示出來,這樣開發(fā)者在跳進(jìn)源代碼中就能知道類或者方法的作用。說到這里可能還是有很多人不能認(rèn)同這種觀點(diǎn),還是認(rèn)識(shí)不到Javadoc的作用。我們看一下Spring框架,隨便打開一個(gè)文件可以看到一般注釋內(nèi)容都要比代碼量多,有的時(shí)候注釋量占整個(gè)文件內(nèi)容的2/3。有人還是認(rèn)為Spring是大框架,每個(gè)Java項(xiàng)目都在用他們寫的好事應(yīng)該的,我們公司自己的項(xiàng)目就我們公司幾個(gè)人看,沒必要花時(shí)間去寫多余的Javadoc,那你是不是該這么認(rèn)為了Spring大廠中的頂尖大牛都這么做,我們小菜鳥是不是對(duì)自己要求更嚴(yán)格一些,向大牛去學(xué)習(xí)呢?!假如在公司A程序員寫了Javadoc,B程序員只寫功能不寫Javadoc不寫注釋,那么一般會(huì)認(rèn)為A程序員會(huì)比B程序員做的好。認(rèn)識(shí)不到Javadoc的作用就像認(rèn)識(shí)不到設(shè)計(jì)模式的作用一樣,很多人都認(rèn)識(shí)不到設(shè)計(jì)模式的作用,認(rèn)為將簡單問題復(fù)雜化,你看一下每個(gè)大框架都會(huì)用到多種設(shè)計(jì)模式,如果只知道寫增刪改查,再工作幾年仍然不會(huì)有提高。
一:簡介
Javadoc用于描述類或者方法的作用。Javadoc可以寫在類上面和方法上面。
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html
二:寫在類上面的Javadoc
寫在類上的文檔標(biāo)注一般分為三段:
- 第一段:概要描述,通常用一句或者一段話簡要描述該類的作用,以英文句號(hào)作為結(jié)束
- 第二段:詳細(xì)描述,通常用一段或者多段話來詳細(xì)描述該類的作用,一般每段話都以英文句號(hào)作為結(jié)束
- 第三段:文檔標(biāo)注,用于標(biāo)注作者、創(chuàng)建時(shí)間、參閱類等信息
第一段:概要描述
單行示例:
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)以@開頭東東被稱之為Javadoc文檔標(biāo)記,是JDK定義好的,如@author、@version、@since、@see、@link、@code、@param、@return、@exception、@throws等。
1. @link:{@link 包名.類名#方法名(參數(shù)類型)} 用于快速鏈接到相關(guān)代碼
@link的使用語法{@link 包名.類名#方法名(參數(shù)類型)},其中當(dāng)包名在當(dāng)前類中已經(jīng)導(dǎo)入了包名可以省略,可以只是一個(gè)類名,也可以是僅僅是一個(gè)方法名,也可以是類名.方法名,使用此文檔標(biāo)記的類或者方法,可用通過按住Ctrl鍵+單擊 可以快速跳到相應(yīng)的類或者方法上,解析成html其實(shí)就是使用< code> 包名.類名#方法名(參數(shù)類型)< /code>
@link示例
// 完全限定的類名
{@link java.lang.Character}
// 省略包名
{@link String}
// 省略類名,表示指向當(dāng)前的某個(gè)方法
{@link #length()}
// 包名.類名.方法名(參數(shù)類型)
{@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中只要涉及到類名或者方法名,都需要使用@code進(jìn)行標(biāo)記。
第二段:詳細(xì)描述
詳細(xì)描述一般用一段或者幾個(gè)鍛煉來詳細(xì)描述類的作用,詳細(xì)描述中可以使用html標(biāo)簽,如<p>、<pre>、<a>、<ul>、<i>等標(biāo)簽, 通常詳細(xì)描述都以段落p標(biāo)簽開始。
詳細(xì)描述和概要描述中間通常有一個(gè)空行來分割
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)簽來標(biāo)記,凡涉及到類名和方法名都用@code標(biāo)記,凡涉及到組織的,一般用a標(biāo)簽提供出來鏈接地址。
3. @param
一般類中支持泛型時(shí)會(huì)通過@param來解釋泛型的類型
/**
* @param <E> the type of elements in this list
*/
public interface List<E> extends Collection<E> {}
4. @author
詳細(xì)描述后面一般使用@author來標(biāo)記作者,如果一個(gè)文件有多個(gè)作者來維護(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)記該類相關(guān)聯(lián)的類,@see即可以用在類上,也可以用在方法上。
/**
* @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 從以下版本開始
@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 {}
三:寫在方法上的Javadoc
寫在方法上的文檔標(biāo)注一般分為三段:
- 第一段:概要描述,通常用一句或者一段話簡要描述該方法的作用,以英文句號(hào)作為結(jié)束
- 第二段:詳細(xì)描述,通常用一段或者多段話來詳細(xì)描述該方法的作用,一般每段話都以英文句號(hào)作為結(jié)束
- 第三段:文檔標(biāo)注,用于標(biāo)注參數(shù)、返回值、異常、參閱等
方法詳細(xì)描述上經(jīng)常使用html標(biāo)簽來,通常都以p標(biāo)簽開始,而且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è)常見應(yīng)用就是用來表示計(jì)算機(jī)的源代碼。
一般p經(jīng)常結(jié)合pre使用,或者pre結(jié)合@code共同使用(推薦@code方式)
一般經(jīng)常使用pre來舉例如何使用方法
注意: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 跟異常類型 異常描述 , 用于描述方法內(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既可以用來類上也可以用在方法上,表示可以參考的類或者方法
/**
* @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用于注解在重寫方法或者子類上,用于繼承父類中的Javadoc
- 基類的文檔注釋被繼承到了子類
- 子類可以再加入自己的注釋(特殊化擴(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;
/**
* 類 {@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.dhdzp.com/article/192943.htm
通過IDEA生成Javadoc: Tools --> Generate JavaDoc -->
注意要配置編碼,如果不配送為生成亂碼,還需要配置Output directory




到此這篇關(guān)于Javadoc 具體使用詳解的文章就介紹到這了,更多相關(guān)Javadoc 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成itext實(shí)現(xiàn)html轉(zhuǎn)PDF
iText是著名的開放源碼的站點(diǎn)sourceforge一個(gè)項(xiàng)目,是用于生成PDF文檔的一個(gè)java類庫,本文主要介紹了如何利用itext實(shí)現(xiàn)html轉(zhuǎn)PDF,需要的可以參考下2024-03-03
SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法
本篇文章主要介紹了SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Java import static及import原理區(qū)別解析
這篇文章主要介紹了Java import static及import原理區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
SpringBoot集成redis實(shí)現(xiàn)分布式鎖的示例代碼
這篇文章主要介紹了SpringBoot集成redis實(shí)現(xiàn)分布式鎖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringBoot項(xiàng)目部署到服務(wù)器的兩種方式
目前,前后端分離的架構(gòu)已成主流,而使用SpringBoot構(gòu)建Web應(yīng)用是非??焖俚?項(xiàng)目發(fā)布到服務(wù)器上的時(shí)候,只需要打成一個(gè)jar包,然后通過命令 : java -jar jar包名稱即可啟動(dòng)服務(wù)了,本文介紹了SpringBoot項(xiàng)目部署到服務(wù)器的兩種方式,需要的朋友可以參考下2024-10-10
Springboot使用sharedingjdbc實(shí)現(xiàn)分庫分表
這篇文章主要介紹了Springboot使用sharedingjdbc實(shí)現(xiàn)分庫分表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
詳解Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí)
這篇文章主要為大家詳細(xì)介紹了Java對(duì)象結(jié)構(gòu)與對(duì)象鎖的升級(jí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

