您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java编码规范(常用重点)

2025/12/13 16:11:45发布8次查看
下面是我整理的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 */
类描述:描述类的功能,单行直接写,如果多行需要使用<p></p>
@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编码规范(常用重点)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product