JAVA代码注释规范

Java类注释规范
本文详细介绍了Java类文件的各种注释规范,包括类文件注释、类功能注释、类变量注释、类方法注释及方法内代码块注释等,提供了丰富的示例帮助理解和应用。

转载地址

 

整个类文件注释 

示例如下:
 
Java代码
  1. /*
  2. * @(#)Object.java      1.61 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */  
  7.   
  8.   
  9.   
  10. package java.lang;  
[java]  view plain  copy
  1. /* 
  2.  
  3.  * @(#)Object.java     1.61 03/01/23 
  4.  
  5.  * 
  6.  
  7.  * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 
  8.  
  9.  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
  10.  
  11.  */  
  12.   
  13.    
  14.   
  15. package java.lang;  
注释结构:
  1. /*
  2. * @(#){类名称}.java        {创建时间}
  3. *
  4. * {某人或某公司具有完全的版权}
  5. * {使用者必须经过许可}
  6. */  
  7.   
  8.   
  9.   
  10. package java.lang;   
  11.   
  12.    
[java]  view plain  copy
  1. /* 
  2.  
  3.  * @(#){类名称}.java       {创建时间} 
  4.  
  5.  * 
  6.  
  7.  * {某人或某公司具有完全的版权} 
  8.  
  9.  * {使用者必须经过许可} 
  10.  
  11.  */  
  12.   
  13. package java.lang;  
2. 具体类功能注释 

示例如下: 
Java代码
  1. /**
  2. * Class <code>Object</code> is the root of the class hierarchy.
  3. * Every class has <code>Object</code> as a superclass. All objects,
  4. * including arrays, implement the methods of this class.
  5. *
  6. * @author   unascribed
  7. * @version 1.61, 01/23/03
  8. * @see      java.lang.Class
  9. * @since    JDK1.0
  10. */  
  11.   
  12. public class Object {}   
  13.   
  14.    
[java]  view plain  copy
  1. /** 
  2.  
  3.  * Class <code>Object</code> is the root of the class hierarchy. 
  4.  
  5.  * Every class has <code>Object</code> as a superclass. All objects, 
  6.  
  7.  * including arrays, implement the methods of this class. 
  8.  
  9.  * 
  10.  
  11.  * @author  unascribed 
  12.  
  13.  * @version 1.61, 01/23/03 
  14.  
  15.  * @see     java.lang.Class 
  16.  
  17.  * @since   JDK1.0 
  18.  
  19.  */  
  20.   
  21. public class Object {}  
注释结构: 
Java代码
  1. /**
  2. * 类 <code>{类名称}</code>{此类功能描述}
  3. *
  4. * @author   {作者}
  5. * @version {版本,常用时间代替}
  6. * @see      java.lang.Class
  7. * @since    JDK{jdk版本}
  8. */  
  9.   
  10. public class Object {}   
  11.   
  12.    
[java]  view plain  copy
  1. /** 
  2.  
  3.  * 类 <code>{类名称}</code>{此类功能描述} 
  4.  
  5.  * 
  6.  
  7.  * @author  {作者} 
  8.  
  9.  * @version {版本,常用时间代替} 
  10.  
  11.  * @see     java.lang.Class 
  12.  
  13.  * @since   JDK{jdk版本} 
  14.  
  15.  */  
  16.   
  17. public class Object {}  
3. 类变量注释 

示例如下: 
Java代码
  1. /** The value is used for character storage. */  
  2.   
  3. private char value[];   
  4.   
  5.    
[java]  view plain  copy
  1. /** The value is used for character storage. */  
  2.   
  3.  private char value[];  
注释结构: 
Java代码
  1. /** {此值是用来存储/记录什么的}*/  
  2.   
  3. private String str ;   
[java]  view plain  copy
  1. /** {此值是用来存储/记录什么的}*/  
  2.   
  3.  private String str ;  
4. 类方法注释 

示例如下: 
Java代码
  1.     /**
  2.       * Returns a new string that is a substring of this string. The
  3.       * substring begins with the character at the specified index and
  4.       * extends to the end of this string. <p>
  5.       * Examples:
  6.       * <blockquote><pre>
  7.       * "unhappy".substring(2) returns "happy"
  8.       * "Harbison".substring(3) returns "bison"
  9.       * "emptiness".substring(9) returns "" (an empty string)
  10.       * </pre></blockquote>
  11.       *
  12.       * @param       beginIndex    the beginning index, inclusive.
  13.       * @return      the specified substring.
  14.       * @exception   IndexOutOfBoundsException   if
  15.       *              <code>beginIndex</code> is negative or larger than the
  16.       *              length of this <code>String</code> object.
  17.       */  
  18.   
  19. public String substring(int beginIndex) {   
  20.   
  21. return substring(beginIndex, count);   
  22.   
  23. }   
  24.   
  25.    
[java]  view plain  copy
  1. /** 
  2.  
  3.      * Returns a new string that is a substring of this string. The 
  4.  
  5.      * substring begins with the character at the specified index and 
  6.  
  7.      * extends to the end of this string. <p> 
  8.  
  9.      * Examples: 
  10.  
  11.      * <blockquote><pre> 
  12.  
  13.      * "unhappy".substring(2) returns "happy" 
  14.  
  15.      * "Harbison".substring(3) returns "bison" 
  16.  
  17.      * "emptiness".substring(9) returns "" (an empty string) 
  18.  
  19.      * </pre></blockquote> 
  20.  
  21.      * 
  22.  
  23.      * @param      beginIndex   the beginning index, inclusive. 
  24.  
  25.      * @return     the specified substring. 
  26.  
  27.      * @exception  IndexOutOfBoundsException  if 
  28.  
  29.      *             <code>beginIndex</code> is negative or larger than the 
  30.  
  31.      *             length of this <code>String</code> object. 
  32.  
  33.      */  
  34.   
  35. public String substring(int beginIndex) {  
  36.   
  37. return substring(beginIndex, count);  
  38.   
  39. }  
注释结构: 
Java代码
  1.     /**
  2.       * {方法的功能/动作描述}
  3.       *
  4.       * @param       {引入参数名}    {引入参数说明}
  5.       * @return       {返回参数名}    {返回参数说明}
  6.       * @exception    {说明在某情况下,将发生什么异常}
  7.       */  
  8.   
  9. public String substring(int beginIndex) {   
  10.   
  11. return substring(beginIndex, count);   
  12.   
  13. }   
  14.   
  15.    
[java]  view plain  copy
  1. /** 
  2.  
  3.      * {方法的功能/动作描述} 
  4.  
  5.      * 
  6.  
  7.      * @param      {引入参数名}   {引入参数说明} 
  8.  
  9.      * @return      {返回参数名}   {返回参数说明} 
  10.  
  11.      * @exception   {说明在某情况下,将发生什么异常} 
  12.  
  13.      */  
  14.   
  15. public String substring(int beginIndex) {  
  16.   
  17. return substring(beginIndex, count);  
  18.   
  19. }  
5.     类方法中代码块注释 

示例如下: 
Java代码
  1. /*
  2. * 调用持久化类,将数据保存到库
  3. *
  4. * 判断是添加,还是修改
  5. */  
  6.   
  7. boolean ifSucc = false;   
  8.   
  9. if(request.getParameter("YINGLI_ID")==null){   
  10.   
  11.         String GUID = new RandomGUID().toString();   
  12.   
  13.         stressTestDataBean.setUSER_ID(Integer.toString(userId));   
  14.   
  15.         stressTestDataBean.setSIGN_ISBN((String)vSectNum.get(0));   
  16.   
  17.         stressTestDataBean.setSHENHE_JIEGUO("0");   
  18.   
  19.         stressTestDataBean.setGUID(GUID);   
  20.   
  21.         stressTestDataBean.setCREATE_DATE("getdate()");   
  22.   
  23.         stressTestDataBean.setSTATE("A");   
  24.   
  25.                                             
  26.   
  27.         ifSucc = StressTestDataDao.addStressTestData(db,stressTestDataBean);   
  28.   
  29. }else{   
  30.   
  31.         ifSucc = StressTestDataDao.mendStressTestData(db,stressTestDataBean);   
  32.   
  33. }   
  34.   
  35.    
[java]  view plain  copy
  1. /* 
  2.  
  3. * 调用持久化类,将数据保存到库 
  4.  
  5. * 
  6.  
  7. * 判断是添加,还是修改 
  8.  
  9. */  
  10.   
  11. boolean ifSucc = false;  
  12.   
  13. if(request.getParameter("YINGLI_ID")==null){  
  14.   
  15.        String GUID = new RandomGUID().toString();  
  16.   
  17.        stressTestDataBean.setUSER_ID(Integer.toString(userId));  
  18.   
  19.        stressTestDataBean.setSIGN_ISBN((String)vSectNum.get(0));  
  20.   
  21.        stressTestDataBean.setSHENHE_JIEGUO("0");  
  22.   
  23.        stressTestDataBean.setGUID(GUID);  
  24.   
  25.        stressTestDataBean.setCREATE_DATE("getdate()");  
  26.   
  27.        stressTestDataBean.setSTATE("A");  
  28.   
  29.                                            
  30.   
  31.        ifSucc = StressTestDataDao.addStressTestData(db,stressTestDataBean);  
  32.   
  33. }else{  
  34.   
  35.        ifSucc = StressTestDataDao.mendStressTestData(db,stressTestDataBean);  
  36.   
  37. }  
注释结构: 
Java代码
  1. /*
  2. * {功能描述}
  3. *
  4. * {具体实现动作}
  5. */  
  6.   
  7. boolean ifSucc = false;   
  8.   
  9. if(request.getParameter("YINGLI_ID")==null){   
  10.   
  11.         String GUID = new RandomGUID().toString();   
  12.   
  13.         stressTestDataBean.setUSER_ID(Integer.toString(userId));   
  14.   
  15.         stressTestDataBean.setSIGN_ISBN((String)vSectNum.get(0));   
  16.   
  17.         stressTestDataBean.setSHENHE_JIEGUO("0");   
  18.   
  19.         stressTestDataBean.setGUID(GUID);   
  20.   
  21.         stressTestDataBean.setCREATE_DATE("getdate()");   
  22.   
  23.         stressTestDataBean.setSTATE("A");   
  24.   
  25.                                             
  26.   
  27.         ifSucc = StressTestDataDao.addStressTestData(db,stressTestDataBean);   
  28.   
  29. }else{   
  30.   
  31.         ifSucc = StressTestDataDao.mendStressTestData(db,stressTestDataBean);   
  32.   
  33. }  
### 阿里巴巴 Java 代码注释规范 在阿里巴巴的 Java 开发编码规范中,对于代码注释的要求非常严格,目的是为了提高代码可读性和维护性。以下是关于注释的一些具体规定: #### 单行注释 单行注释应以 `//` 开头,并且与被注释的内容保持一定的逻辑关系。如果注释内容较长,可以将其单独放在一行上[^1]。 ```java int a = 1; // 对于简单的变量定义,可以在同一行写明含义 // 如果注释较为复杂或者需要解释更多细节,则建议将注释独立成一行 String message = "Hello, world!"; ``` #### 多行注释 多行注释应当使用 `/* */` 的形式包裹起来。当一段代码的功能较复杂时,应在该段代码之前添加详细的说明。 ```java /* * 下面的方法用于计算两个整数的最大公约数。 * 参数 a 和 b 表示待比较的两个正整数。 */ public static int gcd(int a, int b) { while (b != 0) { int temp = a % b; a = b; b = temp; } return a; } ``` #### 和方法的文档注释 、接口以及公共方法都必须提供 Javadoc 文档注释。这些注释应该清晰描述其功能、参数意义及其返回值等信息。 ```java /** * 此表示一个简单的计算器工具,支持基本运算操作。 * * @author Your Name * @version 1.0 */ public class Calculator { /** * 计算两数之和。 * * @param num1 加数一 * @param num2 加数二 * @return 返回相加之和 */ public double add(double num1, double num2) { return num1 + num2; } } ``` #### 特殊情况下的 TODO 注释 对于尚未完成的部分或是存在潜在问题的地方,可以通过 `TODO` 来标记提醒开发者后续处理。 ```java // TODO: 当前实现仅适用于非负整数场景,需扩展至任意实数范围 if (number >= 0) { result = Math.sqrt(number); } else { throw new IllegalArgumentException("Negative numbers are not supported."); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值