全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java注释示例一关键字:Java 程序设计 注释 示例 技巧 原则序言世界级软件大师Kent Beck(大名鼎鼎的JUnit作者)在其著作Implementation Patterns中指出”Programs are read more often than they are written.”大意是“代码被阅读的时间远比写出来的时间要多得多”。好的注释能够帮助代码阅读者了解设计者思路,便于代码的维护和扩展。以下示例是从一些开源框架中摘录的代码片段,供有志于编写可阅读代码的开发人员参考。1. 解释为什么,而非是什么以下代码解释了月份减一和加一的原因,非常简洁明了。public int getMonth() return normalize().getMonth() - 1; / adjust 1-based to 0-based public void setMonth(int month) int y = 0;if (month = 12) y = month / 12; month %= 12; else if (month 0) y = CalendarUtils.floorDivide(month, 12); month = CalendarUtils.mod(month, 12); BaseCalendar.Date d = getCalendarDate();if (y != 0) d.setNormalizedYear(d.getNormalizedYear() + y);d.setMonth(month + 1); / adjust 0-based to 1-based month numbering2. 增强变量含义说明以下代码对变量doc作了增强说明,该注释表明代码是将文档是从2.0版本转换为3.0版本。/ Convert from 2.0 to 3.0 PDLDocument doc30 = convert(doc);3. 给代码划分段落如果一个方法的实现比较复杂,可以通过注释分段解释功能。如以下代码分为三个阶段,一个阶段用一段注释加以阐述。通常更好的做法是将一个复杂的方法重构为多个功能明确的方法。public void execute(ExecutionContext executionContext) Token token = executionContext.getToken(); / phase one: collect all the transitionNames Collection transitionNames = null; List forkedTokens = new ArrayList(); / by default, the fork spawns a token for each leaving transition if (script=null) transitionNames = getLeavingTransitionsMap().keySet(); else / a script is specified / if a script is specified, use that script to calculate the set / of leaving transitions to be used for forking tokens. Map outputMap = null; try outputMap = script.eval(token); catch (Exception e) this.raiseException(e, executionContext); if (outputMap.size()=1) Object result = outputMap.values().iterator().next(); if (result instanceof Collection) transitionNames = (Collection) result; if (transitionNames=null) throw new JbpmException(script for fork +name+ should produce one collection (in one writable variable): +transitionNames); / phase two: create forked tokens for the collected transition names Iterator iter = transitionNames.iterator(); while (iter.hasNext() String transitionName = (String) iter.next(); forkedTokens.add(createForkedToken(token, transitionName); / phase three: launch child tokens from the fork over the given transitions iter = forkedTokens.iterator(); while( iter.hasNext() ) ForkedToken forkedToken = (ForkedToken) iter.next(); Token childToken = forkedToken.token; String leavingTransitionName = forkedToken.leavingTransitionName; ExecutionContext childExecutionContext = new ExecutionContext(childToken); if (leavingTransitionName!=null) leave(childExecutionContext, leavingTransitionName); else leave(childExecutionContext); 4. 说明特殊的异常处理以下代码中注释解释了异常处理。try Class nodeClass = ClassLoaderUtil.getClassLoader().loadClass(className); types.put(elementTag, nodeClass); catch (Exception exception) / NOTE that Errors are not caught because that might halt the JVM and mask the original Error.if (!org.jboss.seam.jbpm.Page.equals(className) log.debug(node +elementTag+ will not be available. class +className+ couldnt be loaded); 5. 解释边界条件和操作布尔变量通常边界条件和操作布尔变量比较难以理解,增加注释可以使代码一目了然。 public void removeTaskInstanceSynchronization(Token token) TaskMgmtInstance tmi = getTaskMgmtInstance(token); Collection taskInstances = tmi.getTaskInstances(); if (taskInstances!=null) Iterator iter = taskInstances.iterator(); while (iter.hasNext() TaskInstance taskInstance = (TaskInstance) iter.next(); if (token.equals(taskInstance.getToken() / remove signalling if (taskInstance.isSignalling() taskInstance.setSignalling(false); / remove blocking if (taskInstance.isBlocking() taskInstance.setBlocking(false); / if this is a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 钻井液工风险评估评优考核试卷含答案
- 气动元件制造工岗中环保竞赛考核试卷含答案
- 2026中考第一轮复习(知识能力+解题思路+易错警示+真题演练)第9课时:方程与不等式的应用(学生版+教师版合并)
- 2026年春招:中国农业发展银行题库及答案
- 广播试题及答案解析
- 化危作业试题及答案详解
- 卷三考试题目及答案解析
- 2026年新能源汽车动力电池及零部件项目可行性研究报告
- 项目9 智能物流分拣-具身机器人系统的软硬协同开发
- 瓷砖铺贴排版美观均匀施工技术
- 齿轮加工工艺规程
- 2025中国电信校招试题及答案
- 手术室电外科设备安全使用
- 南城香培训课件
- 篮球基本动作教学课件
- DB13(J)-T 8446-2021 建筑施工安全技术资料管理标准
- 数字智慧方案5299丨华为业务变革框架及战略级项目管理
- 奔驰官方购车合同协议
- 中国矿产资源集团大数据有限公司招聘笔试题库2025
- 《身边的数据》名师课件
- .高速公路运营企业双重预防机制建设指导手册
评论
0/150
提交评论