Java編碼規範

來源:互聯網
上載者:User

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.