您的位置:首页 > 编程语言 > Java开发

Java编码规范

2016-01-21 02:35 357 查看

命名规范

类命名规范

类中的每个单词的首字母都需要大写,如
UserService
,错误的命名方式
userService、userservice


测试用例以
Test
结尾,如
UserServiceTest


如果以术语缩写开头,术语缩写应全部大写,如
HTMLEditor
错误的写法··

类名应使用英文字母或数字,不应出现特殊字符

接口不以I开头

方法命名规范

第一个单词的首字母小写,其他单词首字母大写

从方法名上应该能看出方法的作用

编码规范

代码缩进

代码缩进为一个tab(4个空格的长度)。Eclipse默认为4个空格的长度。

作用域

类中的属性应设置为私有,通过提供get和set方法实现外部类对私有属性的修改。

为什么要使用Get和Set访问器

如果类中的方法仅供类内部使用应设置为private;如果可以供子类使用应设置为protected;如果是公共方法则应设置为public。

注释规范

版权信息注释

版权信息注释在文件的开头,用于声明代码的版权。使用/**/这样的注释方式。

[code]/* 
 * Copyright ©  2015 TIAMAES Inc. All rights reserved.
 */
package com.tiamaes.gjds.das.controller;


注释模版如下,Window->Preferences->Java->Code Style->Comments->Files

[code]/* 
 * Copyright ©  ${year} TIAMAES Inc. All rights reserved.
 */


Copyright © 2015 TIAMAES Inc. All rights reserved.说明如下

版权 2015 天迈科技股份呢有限公司 保留所有权利。

- Inc. 根据公司法组成的股份有限公司

- Co. Ltd 有限责任公司

类注释规范

类注释信息中应包含,类的描述信息,作者信息及版本信息。

[code]/**  
 * 类描述
 * @author 王成委
 * @since 1.0
 * @see xxx
 */
public class TypeName


注释模版如下,Window->Preferences->Java->Code Style->Comments->Types

[code]/**  
 * ${todo}
 * @author 王成委
 * @since 1.0
 */


类描述:描述类的功能,单行直接写,如果多行需要使用
<p></p>


@author:多个作者使用多个@author

@since:说明此类是从那个版本开始

@see:与类相关的其他类或方法

可参考org.springframework.stereotype.Controller的类注释信息。

[code]/**
 * Indicates that an annotated class is a "Controller" (e.g. a web controller).
 *
 * <p>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
 */


方法注释

使用下面的模版

[code]/**
 * ${todo}
 * ${tags}
 */


${tags}:自动生成参数、异常、返回值等注解

[code]/**
 * TODO
 * @param request
 * @throws IOException
 */
@RequestMapping("/ctx")
public void test(HttpServletRequest request) throws IOException


如果类中的方法实现了抽象方法或重写了父类的方法,应在方法上加上
@Override
注解。如果要覆盖父类方法的注释可以使用
/** */
注释来覆盖父类的注释。

Note

一般方法的注释应写在接口中。

[code]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;


在实现的方法上使用
/**...*/
可以覆盖父类方法的注释。

[code]org.springframework.core.io.AbstractResource
/**
 * This implementation throws a FileNotFoundException, assuming
 * that the resource cannot be resolved to an absolute file path.
 */
@Override
public File getFile() throws IOException {
    throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
}


属性和变量及方法内代码的注释

使用
//
来对变量和属性注释,方法内的代码也使用
//
注释

如非必要变量和属性可以不加注释,如果代码内有负责逻辑应使用
//
注释加以说明

[code]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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: