Java編碼規範
命名規範類命名規範類中的每個單詞的首字母都需要大寫,如UserService,錯誤的命名方式userService、userservice 測試案例以Test結尾,如UserServiceTest 如果以術語縮寫開頭,術語縮寫應全部大寫,如HTMLEditor 錯誤的寫法·· 類名應使用英文字母或數字,不應出現特殊字元 介面不以I開頭方法命名規範第一個單詞的首字母小寫,其他單字首大寫 從方法名上應該能看出方法的作用編碼規範代碼縮排
代碼縮排為一個tab(4個空格的長度)。Eclipse預設為4個空格的長度。
範圍
類中的屬性應設定為私人,通過提供get和set方法實現外部類對私人屬性的修改。
如果類中的方法僅供類內部使用應設定為private;如果可以供子類使用應設定為protected;如果是公用方法則應設定為public。
注釋規範著作權資訊注釋
著作權資訊注釋在檔案的開頭,用於聲明代碼的著作權。使用/**/這樣的注釋方式。
/* * Copyright ? 2015 TIAMAES Inc. All rights reserved. */package com.tiamaes.gjds.das.controller;
注釋模版如下,Window->Preferences->Java->Code Style->Comments->Files
/* * Copyright ? ${year} TIAMAES Inc. All rights reserved. */
Copyright ? 2015 TIAMAES Inc. All rights reserved.說明如下
著作權 2015 天邁科技股份呢有限公司 著作權所有,並保留一切權利。
- Inc. 根據公司法組成的股份有限公司
- Co. Ltd 有限責任公司
類注釋規範
類注釋資訊中應包含,類的描述資訊,作者資訊及版本資訊。
/** * 類描述 * @author 王成委 * @since 1.0 * @see xxx */public class TypeName
注釋模版如下,Window->Preferences->Java->Code Style->Comments->Types
/** * ${todo} * @author 王成委 * @since 1.0 */
類描述:描述類的功能,單行直接寫,如果多行需要使用
@author:多個作者使用多個@author
@since:說明此類是從那個版本開始
@see:與類相關的其他類或方法
可參考org.springframework.stereotype.Controller的類注釋資訊。
/** * Indicates that an annotated class is a "Controller" (e.g. a web controller). * *
This annotation serves as a specialization of {@link Component @Component}, * allowing for implementation classes to be autodetected through classpath scanning. * It is typically used in combination with annotated handler methods based on the * {@link org.springframework.web.bind.annotation.RequestMapping} annotation. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 2.5 * @see Component * @see org.springframework.web.bind.annotation.RequestMapping * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner */
方法注釋
使用下面的模版
/** * ${todo} * ${tags} */
${tags}:自動產生參數、異常、傳回值等註解
/** * TODO * @param request * @throws IOException */@RequestMapping("/ctx")public void test(HttpServletRequest request) throws IOException
如果類中的方法實現了抽象方法或重寫了父類的方法,應在方法上加上@Override註解。如果要覆蓋父類方法的注釋可以使用/** */注釋來覆蓋父類的注釋。
Note
一般方法的注釋應寫在介面中。
org.springframework.core.io.Resource/** * Return a File handle for this resource. * @throws IOException if the resource cannot be resolved as absolute * file path, i.e. if the resource is not available in a file system */File getFile() throws IOException;
在實現的方法上使用/**...*/可以覆蓋父類方法的注釋。
org.springframework.core.io.AbstractResource/** * This implementation throws a FileNotFoundException, assuming * that the resource cannot be resolved to an absolute file path. */@Overridepublic File getFile() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");}
屬性和變數及方法內代碼的注釋
使用//來對變數和屬性注釋,方法內的代碼也使用//注釋
如非必要變數和屬性可以不加註釋,如果代碼內有負責邏輯應使用//注釋加以說明
public ServletContextResource(ServletContext servletContext, String path) { // check ServletContext Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext"); this.servletContext = servletContext; // check path Assert.notNull(path, "Path is required"); String pathToUse = StringUtils.cleanPath(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse;}