更新時間:2020-06-11 15:47:03 來源:動力節(jié)點 瀏覽2298次
現(xiàn)在java的出產(chǎn)地sun公司并沒有定義一個java注釋規(guī)范,注釋規(guī)范目前是每個公司自己有自己的一套規(guī)范,主要是為了團隊之間的協(xié)作。
1、基本規(guī)則
1.注釋應該使代碼更加清晰易懂
2.注釋要簡潔明了,只要提供能夠明確理解程序必要的信息就可以了。如果注釋太復雜會影響程序整潔度和閱讀感。
3.注釋不僅描述程序作了什么,還要描述為什么這樣做以及約束。
4.對于一般的getter和setter方法不用注釋。
5.類、接口、構造函數(shù)、方法、全局變量必須添加注釋。字段屬性可以選擇添加簡單注釋。
6.簡單注釋一般不超過10個字。
7.特殊地方必須要添加注釋。比如一下幾個地方:典型算法,代碼不明晰處,在代碼修改處,在循環(huán)和邏輯分支組成代碼處,為他人提供的接口。
2、三種注釋方式
1.單行注釋(single-line)://注釋內(nèi)容
一次只能注釋一行,一般是簡單注釋,用來簡短描述某個變量或屬性,程序塊。
2.塊注釋(block):/*注釋內(nèi)容*/
為了進行多行簡單注釋,一般不使用。
3.文檔注釋:/**注釋內(nèi)容*/
可以使用多行,一般用來對類、接口、成員方法、成員變量、靜態(tài)字段、靜態(tài)方法、常量進行說明。Javadoc可以用它來產(chǎn)生代碼的文檔。為了可讀性,可以有縮進和格式控制。
文檔注釋常采用一些Javadoc標簽進行文檔的特定用途描述,用于幫助Javadoc產(chǎn)生文檔,常用的有:
標簽用途說明
author name類/接口描述代碼的作者,每個作者對應一個標簽。
Description類/接口/方法對類,方法,接口的簡單描述
deprecated類/成員方法說明該API已經(jīng)廢除。
exception name description或 throws name description成員方法描述方法拋出的異常,每一個異常對應一個標簽
param name description成員方法描述成員方法中參數(shù)用途和意義,一個參數(shù)對應一個標簽
return description成員方法描述成員方法的返回值的意義
since類/接口/成員方法描述該API最初出現(xiàn)時間,可以填寫版本號
see ClassName類/接口/成員方法/成員變量用于引用特定的類的成員方法的描述,參考轉向,一般ClassName是包括包名的全名
data類/接口/方法用于顯示類,方法,接口具體創(chuàng)建時間,或者修改時間
version text類/接口版本
inheritDoc類/接口/成員方法繼承的文檔
{ link address}或者 linkplain address text}類/接口/方法用于創(chuàng)建一個指向另一份文檔的超鏈接
3、實例
1.文件注釋
一般作比較詳細描述,而且在同個項目里面統(tǒng)一使用,主要包括:版權聲明,license許可證描述。
示例(來自spring-framework):
/*
*Copyright 2002-2016 the original author or authors.
*
*Licensed under the Apache License,Version 2.0(the"License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing,software
*distributed under the License is distributed on an"AS IS"BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
2.類/接口注釋
類,接口描述,一般作詳細描述。按照常用的說明順序呢排列,主要包括
1.類的描述,以。或.結束。
2.類設計的目標,完成什么樣的功能一般和類的描述合并在一起。
3.<Strong>主要的類使用</Strong>如何使用該類,包括環(huán)境要求,比如線程是否安全,并發(fā)性要求以及使用約束。
4.<Strong>已知的BUG</Strong>
5.描述類的修改歷史:<Strong>修改人+日期+簡單說明</Strong>
6. author作者、 version版本, see參照, since開始版本信息
示例(來自spring-framework):
/**
*Delegate for resolving constructors and factory methods.
*Performs constructor resolution through argument matching.
*
* author Juergen Hoeller
* author Rob Harrop
* author Mark Fisher
* author Costin Leau
* since 2.0
* see#autowireConstructor
* see#instantiateUsingFactoryMethod
* see AbstractAutowireCapableBeanFactory
*/
3.方法注釋
方法描述說明,主要對方法的描述,參數(shù)、返回值、拋出異常進行說明。
示例(來自spring-framework)
/**
*Resolve the factory method in the specified bean definition,if possible.
*{ link RootBeanDefinition#getResolvedFactoryMethod()}can be checked for the result.
* param mbd the bean definition to check
* return a BeanWrapper for the new instance
* throws Exception in case of any kind of processing failure
*/
4.修改注釋
在修改處一定要添加注釋,說明修改人,修改原因,修改內(nèi)容,修改時間
以上就是動力節(jié)點java培訓機構的小編針對“編程基礎技術分享,Java注釋規(guī)范”的內(nèi)容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務。