Java基礎——註釋
阿新 • • 發佈:2021-01-07
文章目錄
當代碼變多,甚至需要多個一起編寫程式碼的時候,我們需要註明程式碼的相關資訊,讓其他人能更好理解程式碼。註釋是給予提示或其他簡簡訊息,當需要給出很多資訊時,就需要文件註釋,這些文字都不被執行。
一般註釋
使用//標識單行註解
//把0賦值給a
int a = 0;
使用/開頭,/結尾,可以編寫多行註解
/*
*把0賦值給a
*再把a加1
*/
int a = 0;
a++;
有時程式碼裡又/*分隔符,這時就不適合使用這種形式的多行註釋
文件註釋
用/**
開頭,*/
結尾,可以進行多行註釋,而且可以使用HTML標籤,用java doc工具提取註釋生成文件。
類註釋
一般表明作者、版本、從哪個版本開始的,再就是可以寫一大段文字來標明這個類的功能。
/**
* @author alex
* @version 1.0
* @see <a href="haxh.com"></>
* @since 1.0
*
*/
public class HelloWorld{
......
}
方法註釋
標明引數、返回值和丟擲的異常,如果沒有異常就不要寫,會報錯
/**
*
* @param nums
* @return int
* @throws Exception
*/
public static int solution(int[] nums) {...}
屬性註釋
/**
*引數a為1
*/
private int a = 1;
生成文件
java doc命令可以把程式碼裡的註釋提取出來變成HTML格式的文件,可以在包所在的目錄下開啟命令列,輸入javadoc 包名
,就自動提取出這個包的所有類的註釋
如果在其他目錄開啟命令列,需要加上-d命令,javadoc -d 目錄 包名
提取多個包的註釋javadoc -d 目錄 包名1 包名2
文件範例:
/**
* A <code>PrintStream</code> adds functionality to another output stream,
* namely the ability to print representations of various data values
* conveniently. Two other features are provided as well. Unlike other output
* streams, a <code>PrintStream</code> never throws an
* <code>IOException</code>; instead, exceptional situations merely set an
* internal flag that can be tested via the <code>checkError</code> method.
* Optionally, a <code>PrintStream</code> can be created so as to flush
* automatically; this means that the <code>flush</code> method is
* automatically invoked after a byte array is written, one of the
* <code>println</code> methods is invoked, or a newline character or byte
* (<code>'\n'</code>) is written.
*
* <p> All characters printed by a <code>PrintStream</code> are converted into
* bytes using the platform's default character encoding. The <code>{@link
* PrintWriter}</code> class should be used in situations that require writing
* characters rather than bytes.
*
* @author Frank Yellin
* @author Mark Reinhold
* @since JDK1.0
*/
public class PrintStream ...