首页 > Java > java教程 > 正文

Java编码规范(常用重点)

亚连
发布: 2018-05-10 10:15:56
原创
2196人浏览过

下面是我整理的Java编码规范,写代码时一定要按照编码规范来编写,这样有便于日常维护。

命名规范

类命名规范

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

测试用例以Test结尾,如UserServiceTest

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

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

接口不以I开头

立即学习Java免费学习笔记(深入)”;

方法命名规范

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

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

编码规范

代码缩进

代码缩进为一个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).
 *
 * <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
 */
登录后复制

方法注释

使用下面的模版

/**
 * ${todo}
 * ${tags}
 */
登录后复制

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

/**
 * TODO
 * @param request
 * @throws IOException
 */
@RequestMapping("/ctx")public void test(HttpServletRequest request) throws IOException
登录后复制

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

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;
}
登录后复制

以上是我所整理的一部分JAVA编码规范,希望今后大家做开发时能够严格按照编码规则来开发,这样有便于日常维护。

以上就是Java编码规范(常用重点)的详细内容,更多请关注php中文网其它相关文章!

豆包AI编程
豆包AI编程

智能代码生成与优化,高效提升开发速度与质量!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号