java注釋規范


前言:
     現在java的出產地sun公司並沒有定義一個java注釋規范,注釋規范目前是每個公司自己有自己的一套規范,主要是為了團隊之間的協作。
1、基本規則
     1.注釋應該使代碼更加清晰易懂
     2.注釋要簡潔明了,只要提供能夠明確理解程序必要的信息就可以了。如果注釋太復雜會影響程序整潔度和閱讀感。
     3.注釋不僅描述程序作了什么,還要描述為什么這樣做以及約束。
     4.對於一般的getter和setter方法不用注釋。
     5.類、接口、構造函數、方法、全局變量必須添加注釋。字段屬性可以選擇添加簡單注釋。
     6.簡單注釋一般不超過10個字。
     7.特殊地方必須要添加注釋。比如一下幾個地方:典型算法,代碼不明晰處,在代碼修改處,在循環和邏輯分支組成代碼處,為他人提供的接口。
2、三種注釋方式
     1.單行注釋(single-line)://注釋內容
      一次只能注釋一行,一般是簡單注釋,用來簡短描述某個變量或屬性,程序塊。
     2.塊注釋(block):/*注釋內容*/
     為了進行多行簡單注釋,一般不使用。
     3.文檔注釋:/**注釋內容 */
     可以使用多行,一般用來對類、接口、成員方法、成員變量、靜態字段、靜態方法、常量進行說明。Javadoc可以用它來產生代碼的文檔。為了可讀性,可以有縮進和格式控制。
     文檔注釋常采用一些Javadoc標簽進行文檔的特定用途描述,用於幫助Javadoc產生文檔,常用的有:
 
標簽 用途 說明
@author name 類/接口 描述代碼的作者,每個作者對應一個標簽。
@Description 類/接口/方法 對類,方法,接口的簡單描述
@deprecated 類/成員方法 說明該API已經廢除。
@exception name description 或 @throws name description 成員方法 描述方法拋出的異常,每一個異常對應一個標簽
@param name description 成員方法 描述成員方法中參數用途和意義,一個參數對應一個標簽
@return description 成員方法 描述成員方法的返回值的意義
@since 類/接口/成員方法 描述該API最初出現時間,可以填寫版本號
@see ClassName 類/接口/成員方法/成員變量 用於引用特定的類的成員方法的描述,參考轉向,一般ClassName是包括包名的全名
@data 類/接口/方法 用於顯示類,方法,接口具體創建時間,或者修改時間
@version text 類/接口 版本
@inheritDoc 類/接口/成員方法 繼承的文檔
{@link address} 或者 @ linkplain address text} 類/接口/方法 用於創建一個指向另一份文檔的超鏈接
 
3、實例
     1.文件注釋
     一般作比較詳細描述,而且在同個項目里面統一使用,主要包括:版權聲明,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>如何使用該類,包括環境要求,比如線程是否安全,並發性要求以及使用約束。
          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.方法注釋
     方法描述說明,主要對方法的描述,參數、返回值、拋出異常進行說明。
     示例(來自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.修改注釋
     在修改處一定要添加注釋,說明修改人,修改原因,修改內容,修改時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM